CS1110. Lecture 6: Function calls

Similar documents
CS1110. Lecture 6: Function calls

CS 1110: Introduction to Computing Using Python Lists and Sequences

Lecture 7. Memory in Python

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

Lecture 4: Defining Functions

CS Lecture 19: Loop invariants

Lecture 10. Memory in Python

Iteration and For Loops

Lecture 10: Lists and Sequences

Conditionals & Control Flow

Lecture 26: Sorting CS 1110 Introduction to Computing Using Python

CS Lecture 18: Card Tricks. Announcements. Slides by D. Gries, L. Lee, S. Marschner, W. White

Lecture 11: Iteration and For-Loops

Lecture 7: Objects (Chapter 15) CS 1110 Introduction to Computing Using Python

CS Lecture 26: Subclasses in Event-driven Programs A7 is out! Get started right away you need time to ask questions.

CS 1110: Introduction to Computing Using Python Loop Invariants

Lecture 2: Variables & Assignments

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

CS1110. Lecture 22: Prelim 2 Review Session. Announcements. Processed prelim regrade requests: on the front table.

Lecture 24: Loop Invariants

Lecture 6: Specifications & Testing

isinstance and While Loops

Lecture 17: Classes (Chapter 15)

Lecture 19: Subclasses & Inheritance (Chapter 18)

Lecture 5. Defining Functions

Lecture 9. Memory and Call Stacks

CS Lecture 25: Models, Views, Controllers, and Games. Announcements

Lecture 4. Defining Functions

Lecture 8. Conditionals & Control Flow

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

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

Lecture 4. Defining Functions

Lecture 12. Lists (& Sequences)

Lecture 17: Classes (Chapter 15)

Lecture 9: Memory in Python

Lecture 5: Strings

PREPARING FOR PRELIM 1

Lecture 3: Functions & Modules

CS 1110, LAB 03: STRINGS; TESTING First Name: Last Name: NetID:

6.01, Spring Semester, 2008 Assignment 3, Issued: Tuesday, February 19 1

CS 1110, LAB 1: EXPRESSIONS AND ASSIGNMENTS First Name: Last Name: NetID:

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

CS 1110, LAB 13: SEQUENCE ALGORITHMS

CS 1110, LAB 3: STRINGS; TESTING

15-780: Problem Set #3

Lecture 8: Conditionals & Control Flow (Sections ) CS 1110 Introduction to Computing Using Python

Lecture 13. For-Loops

CS1 Lecture 5 Jan. 25, 2019

61A Lecture 7. Monday, September 15

CS1 Lecture 5 Jan. 26, 2018

Lecture 20: While Loops (Sections 7.3, 7.4)

61A Lecture 6. Monday, February 2

COMP1730/COMP6730 Programming for Scientists. Functions

Lecture 18: Using Classes Effectively (Chapter 16)

Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors

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

6.S189 Homework 1. What to turn in. Exercise 1.1 Installing Python. Exercise 1.2 Hello, world!

DECOMPOSITION, ABSTRACTION, FUNCTIONS

61A Lecture 4. Monday, September 9

Lecture 22. While Loops

CS Lecture 26: Grab Bag. Announcements

Python Lists 2 CS 8: Introduction to Computer Science Lecture #9

Announcements for this Lecture

Announcements for This Lecture

CSci 127: Introduction to Computer Science

CS150 - Assignment 5 Data For Everyone Due: Wednesday Oct. 16, at the beginning of class

What is an algorithm?

61A Lecture 2. Wednesday, September 4, 2013

Statements 2. a operator= b a = a operator b

Lecture 14. Nested Lists and Dictionaries

Unit 8: Programming Languages CS 101, Fall 2018

PREPARING FOR PRELIM 2

Sorting and Searching

CS Prelim 1 Review Fall 2016

Announcements for This Lecture

CS 221 Lecture. Tuesday, 4 October There are 10 kinds of people in this world: those who know how to count in binary, and those who don t.

COMP s1 Lecture 1

Lecture 1: Introduction, Types & Expressions

CS Introduction to Programming Fall 2016

CS 11 python track: lecture 2

Digital Circuits ECS 371

Lecture 13. For-Loops

Unit 10: Data Structures CS 101, Fall 2018

1 Check it out! : Fundamentals of Programming and Computer Science, Fall Homework 3 Programming: Image Processing

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

CS 1110, LAB 2: ASSIGNMENTS AND STRINGS

! The simplest building blocks of a language. ! Compound elements are built from simpler ones

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

lecture24: Disjoint Sets

EXAM PREPARATION SECTION 1

2010Fa CS10 Online Final Answers Section 1

CS 101: Computer Programming and Utilization. Abhiram Ranade

Error Correction continued and start on FPGA architecture

CSc 110, Autumn Lecture 30: Methods. Adapted from slides by Marty Stepp and Stuart Reges

CS61A Lecture 16. Amir Kamil UC Berkeley February 27, 2013

type name(arg1, arg2,...) { body return }

CS111: PROGRAMMING LANGUAGE II

Lists in Python CS 8: Introduction to Computer Science, Winter 2018 Lecture #10

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #23 Loops: Precedence of Operators

. As x gets really large, the last terms drops off and f(x) ½x

Transcription:

CS1110 Lecture 6: Function calls Announcements Additional space in labs: We have added some space and staffing to the 12:20 and 1:25 labs on Tuesday. There is still space to move into these labs. Printed copies are coming in a few minutes! Assignment 1 is out! Grab a printed copy today, and refer to the same text on the website. This assignment is due February 18, and you will re-submit until it s all correct. Slides by D. Gries, L. Lee, S. Marschner, W. White

How Do Functions Work? function call print add_em(3, 4) To evaluate a function call expression: 2. Assign arguments to arguments The value of the function call expression is the returned value def add_em(x, y): return x + y function name add_em x 3 y 4 call frame Return value: 7

How Do Functions Work? function call print add_em(3, 4) arguments def add_em(x, y): 1 sum = x + y 2 return sum To evaluate a function call expression: 2. Assign arguments to The value of the function call expression is the returned value statement numbers function name add_em:1 x 3 y 4 sum local variables 7 call frame 2 program counter Return value: 7

a = 1 b = 2 swap(a, b) To evaluate a function call expression: 2. Assign arguments to with print, use commas to print several values The value of the function call expression is the returned value def swap(x, y): 1 x = y 2 y = x 3 print 'x, y:', x, y Execute this call on paper. What gets printed out? A: x, y: 1 2 B: x, y: 2 1 C: x, y: 2 2 D: x, y: 1 1 E: I don t know

a = 1 b = 2 swap(a, b) arguments module (global) variables a b 1 2 def swap(x, y): 1 x = y 2 y = x 3 print 'x, y:', x, y To evaluate a function call expression: 2. Assign arguments to The value of the function call expression is the returned value swap:1 program counter x y

a = 1 b = 2 swap(a, b) print 'a, b:', a, b arguments module (global) variables a b 1 2 def swap(x, y): 1 t = x 2 x = y 3 y = t 4 print 'x, y:', x, y To evaluate a function call expression: 2. Assign arguments to local variables The value of the function call expression is the returned value program counter swap:1 x y t

import point p = point.point(1,2,3) q = point.point(3,4,5) swap_x(p, q) print 'p:', p print 'q:', q def swap_x(p, q): 1 t = p.x 2 p.x = q.x 3 q.x = t Execute this code on paper. You will draw 2 objects and a frame. What is in p.x at the end? A: 1 B: 2 C: 3 D: I don t know

import point p = point.point(1,2,3) q = point.point(3,4,5) swap_x(p, q) print 'p:', p print 'q:', q module (global) variables p q id1 id2 def swap_x(p, q): 1 t = p.x 2 p.x = q.x 3 q.x = t id1 Point id2 Point swap_x:1 x 1.0 x 3.0 p q y 2.0 y 4.0 t z 3.0 z 5.0

(9:05 version) s c = 2 print g(3) c 2 def f(x, y): 1 return 3*x + y g:1 a parameter def g(a): 1 b = f(a, c) 2 return f(b, a) b f:1 x y local variable 2. Assign arguments to

(11:15 version) s lt_speed = 3e8 print g(3) def g(m): 1 E = f(m, lt_speed) 2 return E def f(x, y): 1 return x * (y**2) lt_speed 3 10 8 2. Assign arguments to g:1 m E f:1 x y parameter local variable

How to Draw Things variable object variable name old value value class identifier class name class name attributes methods function name : program counter module name local variables call frame

Online Python Tutor module (global) variables pythontutor.com type in whatever code you want controls for stepping through code output from print goes here frame for call of g frame for call of f