Lecture 2 - Names & Functions

Slides:



Advertisements
Similar presentations
2.1 Program Construction In Java
Advertisements

Programming Languages and Paradigms
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 28: Implementing Interpreters.
Fundamentals of Python: From First Programs Through Data Structures
CS61A Lecture 2 Functions and Applicative Model of Computation Tom Magrino and Jon Kotker UC Berkeley EECS June 19, 2012.
Fundamentals of Python: First Programs
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Introduction to Computational Linguistics Programming I.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Introduction. 2COMPSCI Computer Science Fundamentals.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Controlling Program Flow with Decision Structures.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Bill Tucker Austin Community College COSC 1315
Operational Semantics of Scheme
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
Introduction to Python
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Chapter 4 – C Program Control
Environments and the Contour Model
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Topic: Programming Languages and their Evolution + Intro to Scratch
© 2010 David A Watt, University of Glasgow
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
The Metacircular Evaluator
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Coding Concepts (Sub- Programs)
The Metacircular Evaluator
Loops CIS 40 – Introduction to Programming in Python
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
Coding Concepts (Basics)
Lecture 25: Metalinguistics > (meval '((lambda (x) (* x x)) 4)
Chapter 6 Control Statements: Part 2
The Metacircular Evaluator (Continued)
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
6.001 SICP Variations on a Scheme
Introduction to Python
The structure of programming
6.001 SICP Interpretation Parts of an interpreter
Life is Full of Alternatives
Chapter 4 - Program Control
Introduction to Computer Science
Introduction to the Lab
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Using C++ Arithmetic Operators and Control Structures
General Computer Science for Engineers CISC 106 Lecture 03
CSE 190p University of Washington Michael Ernst
Lecture 20 – Practice Exercises 4
Defining Functions.
Control 9 / 25 / 19.
Lecture 6 - Recursion.
Presentation transcript:

Lecture 2 - Names & Functions 09/25 Slides adapted from Berkeley CS61a

Program Structure

Review - Expressions Primitive Expressions: 2 “hello!” add numbers strings names Arithmetic Expressions: 1 + 2 15 // 3 add(3, 4) Call Expressions: max(add(2, 3), 5 * min(-1, 4))

Review - Evaluating Call Expressions add ( 2 , 3 ) Operator Operand Operand Evaluate Evaluate the operator subexpression Evaluate each operand subexpression Apply Apply the value of the operator subexpression to the values of the operand subexpression

Nested Call Expression Evaluate operator Evaluate operands Apply! 1 2 3 45 add(add(6, mul(4, 6)), mul(3, 5)) add 30 15 add(6, mul(4, 6)) mul(3, 5) 24 add 6 mul(4, 6) mul 3 5 mul 4 6 Expression Tree

Values Programs manipulate values Values represent different types of data Integers: 2 44 -3 Strings: “hello!” “cs61a” Floats: 3.14 4.5 -2.0 Booleans: True False

Expressions & Values ‘hello!’ ‘hello!’ 7 / 2 3.5 add(1, max(2, 3)) 4 Expressions evaluate to values in one or more steps Expression: Value: ‘hello!’ ‘hello!’ 7 / 2 3.5 add(1, max(2, 3)) 4

Names Demo 7 Values can be assigned to names to make referring to them easier. A name can only be bound to a single value. x One way to introduce a new name in a program is with an assignment statement. x = 1 + 2 * 3 - 4 // 5 Name Expression Statements affect the program, but do not evaluate to values.

Check Your Understanding >>> f = min >>> f = max >>> g, h = min, max >>> max = g >>> max(f(2, g(h(1, 5), 3)), 4) ???

Visualizing Assignment Demo Names are bound to values in an environment To execute an assignment statement: Evaluate the expression to the right of =. Bind the value of the expression to the name to the left of = in the current environment. Final Value Name Bindings http://pythontutor.com/visualize.html#code=x%20%3D%201%0Ay%20%3D%20x%0Ax%20%3D%202%0Ax,%20y%20%3D%20x%20%2B%20y,%20x&cumulative=true&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false

3 Demo 3 func min(...) 3 4 f(2, g(h(1, 5), 3)) func max(...) 2 3 http://pythontutor.com/visualize.html#code=f%20%3D%20min%0Af%20%3D%20max%0Ag,%20h%20%3D%20min,%20max%0Amax%20%3D%20g%0A&cumulative=true&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false func max(...) 1 5

Functions

Functions Functions allow us to abstract away entire expressions and sequences of computation They take in some input (known as their arguments) and transform it into an output (the return value) We can create functions using def statements. Their input is given in a function call, and their output is given by a return statement. 5 square 25

Defining Functions Demo Function signature indicates name and number of arguments def <name>(<parameters>): return <return expression> def square(x): return x * x y = square(-2) Function body defines the computation performed when the function is applied Execution rule for def Statements Create a function with signature <name>(<parameters>) Set the body of that function to be everything indented after the first line Bind <name> to that function in the current frame

Functions in Environment Diagrams Built-in function User-defined function def statements are a type of assignment that bind names to function values

Calling User-Defined Functions Procedure for calling/applying user-defined functions (for now) Create a new environment frame Bind the function's parameters to its arguments in that frame Execute the body of the function in the new environment def square(x): return x * x square(-2)

Calling User-Defined Functions Procedure for calling/applying user-defined functions (for now) Create a new environment frame Bind the function's parameters to its arguments in that frame Execute the body of the function in the new environment def square(x): return x * x square(-2) Intrinsic name Local frame

Calling User-Defined Functions Procedure for calling/applying user-defined functions (for now) Create a new environment frame Bind the function's parameters to its arguments in that frame Execute the body of the function in the new environment def square(x): return x * x square(-2) Parameter Argument

Calling User-Defined Functions Procedure for calling/applying user-defined functions (for now) Create a new environment frame Bind the function's parameters to its arguments in that frame Execute the body of the function in the new environment def square(x): return x * x square(-2)

Putting it all together def square(x): return x * x Evaluate Evaluate the operator subexpression Evaluate each operand subexpression Apply Apply the value of the operator subexpression to the values of the operand subexpression Operator: square Function: func square(x) square(1 - 3) Operand: 1-3 Argument: -2 Local frame Formal parameter bound to argument

Drawing Environment Diagrams Option 1: Python Tutor (tutor.cs61a.org) Useful for quick visualization or for environment diagram questions Option 2: PythonAnywhere (editor.pythonanywhere.com) Includes an integrated editor/interpreter Good for more complicated code or if you want to debug Developed by Rahul Arya, one of your tutors this summer!

Summary Programs consist of statements, or instructions for the computer, containing expressions, which describe computation and evaluate to values. Values can be assigned to names to avoid repeating computations. An assignment statement assigns the value of an expression to a name in the current environment. Functions encapsulate a series of statements that maps arguments to a return value. A def statement creates a function object with certain parameters and a body and binds it to a name in the current environment. A call expression applies the value of its operator, a function, to the value(s) or its operand(s), some arguments.