Lecture 4. Defining Functions

Similar documents
Lecture 4. Defining Functions

Lecture 4: Defining Functions

Lecture 5. Defining Functions

Lecture 4. Defining Functions

Lecture 4: Defining Functions (Ch ) CS 1110 Introduction to Computing Using Python

Lecture 7. Memory in Python

Lecture 9. Memory and Call Stacks

Lecture 10. Memory in Python

Lecture 3. Functions & Modules

PREPARING FOR PRELIM 1

Lecture 3. Functions & Modules

Lecture 3: Functions & Modules

COMP1730/COMP6730 Programming for Scientists. Functions

CS1 Lecture 13 Feb. 13, 2019

CS1110. Lecture 6: Function calls

Lecture 2: Variables & Assignments

CS 1110 Prelim 1, March 2018

Class Note #02. [Overall Information] [During the Lecture]

Conditionals & Control Flow

Announcements for this Lecture

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

Lecture 6: Specifications & Testing

COMP 250. Lecture 7. Sorting a List: bubble sort selection sort insertion sort. Sept. 22, 2017

CS 1110 Regular Prelim 1, March 14th, 2017

CS1 Lecture 4 Jan. 23, 2019

CS 1110 Prelim 1 October 4th, 2012

CS 1110 Final, December 17th, Question Points Score Total: 100

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Pointers, Arrays and Parameters

Last Name: First: Netid: Section. CS 1110 Final, December 17th, 2014

CS 1110 Final, December 8th, Question Points Score Total: 100

CS 1110, LAB 10: ASSERTIONS AND WHILE-LOOPS 1. Preliminaries

CS 1110 Prelim 1 October 17th, 2013

CSE 11 Style Guidelines

Lecture 2. Variables & Assignment

Introduction to Python

Semester 2, 2018: Lab 1

CS1 Lecture 3 Jan. 18, 2019

Lecture 19. Operators and Abstraction

CS 1110 Final, December 8th, Question Points Score Total: 100

CS1 Lecture 3 Jan. 22, 2018

Previously. Iteration. Date and time structures. Modularisation.

You must pass the final exam to pass the course.

CS 483. Jana Kosecka CS Dept Eng. Building

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

CS1110. Lecture 6: Function calls

CS 1110, LAB 12: SEQUENCE ALGORITHMS First Name: Last Name: NetID:

Lecture 02, Fall 2018 Friday September 7

COMP519 Web Programming Lecture 20: Python (Part 4) Handouts

Last Name: First Name: Cornell NetID, all caps: CS 1110 Regular Prelim 1 Solutions March 2018

PREPARING FOR THE FINAL EXAM

CS1 Lecture 4 Jan. 24, 2018

Use the Associative Property of Multiplication to find the product.

Lecture 13. For-Loops

CMSC201 Computer Science I for Majors

PREPARING FOR THE FINAL EXAM

CS108 Lecture 16: User Defined Classes. Overview/Questions

Lecture 26: Sorting CS 1110 Introduction to Computing Using Python

Short Answer Questions (40 points)

Introduction to: Computers & Programming: Review prior to 1 st Midterm

ENCM 339 Fall 2017 Lecture Section 01 Lab 9 for the Week of November 20

CMSC 201 Fall 2018 Lab 04 While Loops

WELCOME! (download slides and.py files and follow along!) LECTURE 1

Unit 10: Data Structures CS 101, Fall 2018

April 2 to April 4, 2018

Lecture 5: Strings

Only to be used for arranged hours. Order of Operations

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Lab 4 Fruitful Functions

Information Science 1

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Lecture 3. Strings, Functions, & Modules

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

Information Science 1

CS 1110 Prelim 1 October 12th, 2017

VSCode: Open Project -> View Terminal -> npm run pull -> npm start. Lecture 16

CS 1110 Final, December 16th, 2013

Homework notes. Homework 2 grades posted on canvas. Homework 3 due tomorrow. Homework 4 posted on canvas. Due Tuesday, Oct. 3

Lecture 8 Tao Wang 1

CS 1110, LAB 13: SEQUENCE ALGORITHMS

EXPRESSIONS, STATEMENTS, AND FUNCTIONS 1

Lecture 1. Course Overview, Python Basics

CSC148 Recipe for Designing Classes

Quiz 1: Functions and Procedures

QUIZ. 0] Define arrays 1] Define records 2] How are arrays and records: (a) similar? (b) different?

Announcements for the Class

Advanced Object Oriented Programming EECS2030Z

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

Midterm II CS164, Spring 2006

Assignment 7: Due Wednesday May 11 at 6pm UPDATES on Monday May 9

Lecture 1. Course Overview, Python Basics

University of Texas at Arlington, TX, USA

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

PREPARING FOR PRELIM 2

How To Think Like A Computer Scientist, chapter 3; chapter 6, sections

CS 1110 Prelim 2 November 14th, 2013

Computer Science II Lecture 1 Introduction and Background

CS 3030 Scripting Languages Syllabus

Transcription:

Lecture 4 Defining Functions

Academic Integrity Quiz Remember: quiz about the course AI policy Have posted grades for completed quizes Right now, missing ~90 enrolled students If did not receive perfect, take it again If you are not aware of the quiz Go to http://www.cs.cornell.edu/courses/cs11110/ Click Academic Integrity in side bar Read and take quiz in CMS 8/31/17 Defining Functions 2

Recall: Modules Modules provide extra functions, variables Example: math provides math.cos(), math.pi Access them with the import command Python provides a lot of them for us This Lecture: How to make modules Komodo Edit to make a module Python to use the module Two different programs 8/31/17 Defining Functions 3

We Write Programs to Do Things Functions are the key doers Function Call Function Definition Command to do the function >>> plus(23) 24 >>> Defines what function does def plus(n): return n+1 Parameter: variable that is listed within the parentheses of a method header. Argument: a value to assign to the method parameter when it is called 8/31/17 Defining Functions 4

We Write Programs to Do Things Functions are the key doers Function Call Function Definition Command to do the function Defines what function does >>> plus(23) 24 >>> Function Header def plus(n): return n+1 Parameter: variable that is listed within the parentheses of a method header. Argument: a value to assign to the method parameter when it is called 8/31/17 Defining Functions 5

We Write Programs to Do Things Functions are the key doers Function Call Function Definition Command to do the function Defines what function does >>> plus(23) 24 >>> Function Header def plus(n): return n+1 Function Body (indented) Parameter: variable that is listed within the parentheses of a method header. Argument: a value to assign to the method parameter when it is called 8/31/17 Defining Functions 6

We Write Programs to Do Things Functions are the key doers Function Call Function Definition Command to do the function Defines what function does >>> plus(23) 24 >>> argument to assign to n Function Header def plus(n): declaration of parameter n return n+1 Function Body (indented) Parameter: variable that is listed within the parentheses of a method header. Argument: a value to assign to the method parameter when it is called 8/31/17 Defining Functions 7

Anatomy of a Function Definition name parameters def plus(n): """Returns the number n+1 Parameter n: number to add to Precondition: n is a number""" x = n+1 return x Function Header Docstring Specification Statements to execute when called 8/31/17 Defining Functions 8

Anatomy of a Function Definition name parameters def plus(n): """Returns the number n+1 Parameter n: number to add to Precondition: n is a number""" x = n+1 return x Function Header Docstring Specification Statements to execute when called The vertical line indicates indentation Use vertical lines when you write Python on exams so we can see indentation 8/31/17 Defining Functions 9

The return Statement Format: return <expression> Used to evaluate function call (as an expression) Also stops executing the function! Any statements after a return are ignored Example: temperature converter function def to_centigrade(x): """Returns: x converted to centigrade""" return 5*(x-32)/9.0 8/31/17 Defining Functions 10

A More Complex Example Function Definition def foo(a,b): """Return something Param a: number Param b: number""" x = a y = b return x*y+y >>> x = 2 >>> foo(3,4) Function Call x? What is in the box? 8/31/17 Defining Functions 11

A More Complex Example Function Definition def foo(a,b): """Return something Param a: number Param b: number""" x = a y = b return x*y+y >>> x = 2 >>> foo(3,4) Function Call x? What is in the box? A: 2 B: 3 C: 16 D: Nothing! E: I do not know 8/31/17 Defining Functions 12

A More Complex Example Function Definition def foo(a,b): """Return something Param a: number Param b: number""" x = a y = b return x*y+y >>> x = 2 >>> foo(3,4) Function Call x? What is in the box? A: 2 CORRECT B: 3 C: 16 D: Nothing! E: I do not know 8/31/17 Defining Functions 13

Understanding How Functions Work Function Frame: Representation of function call A conceptual model of Python Draw parameters as variables (named boxes) Number of statement in the function body to execute next Starts with 1 function name instruction counter parameters local variables (later in lecture) 8/31/17 Defining Functions 14

Text (Section 3.10) vs. Class Textbook This Class to_centigrade 1 to_centigrade x > 50.0 x 50.0 Definition: def to_centigrade(x): return 5*(x-32)/9.0 Call: to_centigrade(50.0) 8/31/17 Defining Functions 15

Example: to_centigrade(50.0) 1. Draw a frame for the call 2. Assign the argument value to the parameter (in frame) 3. Execute the function body Look for variables in the frame If not there, look for global variables with that name 4. Erase the frame for the call Initial call frame (before exec body) to_centigrade 1 x 50.0 1 def to_centigrade(x): return 5*(x-32)/9.0 next line to execute 8/31/17 Defining Functions 16

Example: to_centigrade(50.0) 1. Draw a frame for the call 2. Assign the argument value to the parameter (in frame) 3. Execute the function body Look for variables in the frame If not there, look for global variables with that name 4. Erase the frame for the call to_centigrade x Executing the return statement 50.0 RETURN 10.0 1 def to_centigrade(x): return 5*(x-32)/9.0 Return statement creates a special variable for result 8/31/17 Defining Functions 17

Example: to_centigrade(50.0) 1. Draw a frame for the call 2. Assign the argument value to the parameter (in frame) 3. Execute the function body Look for variables in the frame If not there, look for global variables with that name 4. Erase the frame for the call to_centigrade x Executing the return statement 50.0 RETURN 10.0 1 def to_centigrade(x): return 5*(x-32)/9.0 The return terminates; no next line to execute 8/31/17 Defining Functions 18

Example: to_centigrade(50.0) 1. Draw a frame for the call 2. Assign the argument value to the parameter (in frame) 3. Execute the function body Look for variables in the frame If not there, look for global variables with that name 4. Erase the frame for the call 1 def to_centigrade(x): return 5*(x-32)/9.0 But don t actually erase on an exam 8/31/17 Defining Functions 19

Call Frames vs. Global Variables The specification is a lie: 1 2 def swap(a,b): """Swap global a & b""" tmp = a a = b Global Variables a 1 b 2 Call Frame 3 b = tmp swap 1 >>> a = 1 >>> b = 2 >>> swap(a,b) a 1 b 2 8/31/17 Defining Functions 20

Call Frames vs. Global Variables The specification is a lie: def swap(a,b): """Swap global a & b""" 1 tmp = a 2 a = b 3 b = tmp >>> a = 1 >>> b = 2 >>> swap(a,b) Global Variables a 1 b 2 Call Frame swap 2 a 1 b 2 tmp 1 8/31/17 Defining Functions 21

Call Frames vs. Global Variables The specification is a lie: def swap(a,b): """Swap global a & b""" 1 tmp = a 2 a = b 3 b = tmp >>> a = 1 >>> b = 2 >>> swap(a,b) Global Variables a 1 b 2 Call Frame swap 3 x a 1 2 b 2 tmp 1 8/31/17 Defining Functions 22

Call Frames vs. Global Variables The specification is a lie: def swap(a,b): """Swap global a & b""" 1 tmp = a 2 a = b 3 b = tmp >>> a = 1 >>> b = 2 >>> swap(a,b) Global Variables a 1 b 2 Call Frame swap x a 1 2 b 2 1 tmp 1 x 8/31/17 Defining Functions 23

Call Frames vs. Global Variables The specification is a lie: def swap(a,b): """Swap global a & b""" 1 tmp = a 2 a = b 3 b = tmp Global Variables a 1 b 2 Call Frame >>> a = 1 >>> b = 2 >>> swap(a,b) 8/31/17 Defining Functions 24

Function Access to Global Space All function definitions are in some module Call can access global space for that module math.cos: global for math temperature.to_centigrade uses global for temperature But cannot change values Assignment to a global makes a new local variable! Why we limit to constants Global Space (for globals.py) get_a 1 # globals.py """Show how globals work""" a = 4 # global space def get_a(): a 4 return a # returns global 8/31/17 Defining Functions 25

Function Access to Global Space All function definitions are in some module Call can access global space for that module math.cos: global for math temperature.to_centigrade uses global for temperature But cannot change values Assignment to a global makes a new local variable! Why we limit to constants Global Space (for globals.py) change_a a # globals.py 3.5 """Show how globals work""" a = 4 # global space def change_a(): a a = 3.5 # local variable return a 4 8/31/17 Defining Functions 26

Exercise Time 1 2 3 Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y >>> x = foo(3,4) Function Call What does the frame look like at the start? 8/31/17 Defining Functions 27

Which One is Closest to Your Answer? A: B: foo 0 foo 1 C: D: foo 1 x 3 foo 1 x y 8/31/17 Defining Functions 28

Which One is Closest to Your Answer? A: B: foo 0 foo 1 C: D: foo 1 x 3 E: \_( ツ )_/ foo 1 x y 8/31/17 Defining Functions 29

Exercise Time 1 2 3 Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y >>> x = foo(3,4) B: Function Call foo 1 8/31/17 Defining Functions 30

Exercise Time 1 2 3 Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y >>> x = foo(3,4) B: Function Call foo 1 What is the next step? 8/31/17 Defining Functions 31

Which One is Closest to Your Answer? A: B: foo 2 foo 1 x 3 C: D: foo 2 x 3 foo 2 x 3 y 8/31/17 Defining Functions 32

Exercise Time 1 2 3 Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y >>> x = foo(3,4) C: Function Call foo 2 x 3 8/31/17 Defining Functions 33

Exercise Time 1 2 3 Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y >>> x = foo(3,4) C: Function Call foo 2 x 3 What is the next step? 8/31/17 Defining Functions 34

Which One is Closest to Your Answer? A: B: foo 3 x 3 y 4 C: D: foo x 3 y 4 RETURN 16 foo x 3 y 4 RETURN 3 8/31/17 Defining Functions 35

Exercise Time 1 2 3 Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y >>> x = foo(3,4) A: Function Call foo 3 x 3 y 4 8/31/17 Defining Functions 36

Exercise Time 1 2 3 Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y >>> x = foo(3,4) A: Function Call foo 3 x 3 y 4 What is the next step? 8/31/17 Defining Functions 37

Which One is Closest to Your Answer? A: B: foo 3 foo RETURN 16 x 3 y 4 RETURN 16 3 C: D: foo x 3 y 4 RETURN 16 8/31/17 Defining Functions 38

Exercise Time 1 2 3 Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y >>> x = foo(3,4) C: foo Function Call x 3 y 4 RETURN 16 8/31/17 Defining Functions 39

Exercise Time 1 2 3 Function Definition def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y >>> x = foo(3,4) C: foo Function Call x 3 y 4 RETURN 16 What is the next step? 8/31/17 Defining Functions 40

Which One is Closest to Your Answer? A: B: foo RETURN 16 C: D: foo x 16 x 16 8/31/17 Defining Functions 41

Exercise Time Function Definition Function Call 1 2 3 def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y >>> x = foo(3,4) D: x 16 8/31/17 Defining Functions 42

Exercise Time Function Definition Function Call 1 2 3 def foo(a,b): """Return something Param x: a number Param y: a number""" x = a y = b return x*y+y >>> x = foo(3,4) D: x Variable in global space 16 8/31/17 Defining Functions 43

Visualizing Frames: The Python Tutor 8/31/17 Defining Functions 44

Visualizing Frames: The Python Tutor Global Space Call Frame 8/31/17 Defining Functions 45

Visualizing Frames: The Python Tutor Global Space Variables from second lecture go in here Call Frame 8/31/17 Defining Functions 46

Visualizing Frames: The Python Tutor Missing line numbers! 8/31/17 Defining Functions 47

Visualizing Frames: The Python Tutor Line number marked here (sort-of) Missing line numbers! 8/31/17 Defining Functions 48

Next Time: Concrete Examples 8/31/17 Defining Functions 49