Docsity

Function Calls - Introduction to Computing Using Python - Lecture Slides, Slides of Introduction to Computing

Toggle sidebar
bg1
x!y!
add_em
3!4!
How Do Functions Work?!
def add_em(x, y):!
return x + y!
!
print add_em(3, 4)!
function call!
function definition!
To evaluate a function call expression:!
1. Create a frame for the call!
2. Assign arguments to parameters!
3. Execute function body!
4. Erase the frame!
The value of the function "
call expression is the returned value!
arguments!parameters!
function name!
parameters!
call frame!Return value: 7!
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Function Calls - Introduction to Computing Using Python - Lecture Slides and more Slides Introduction to Computing in PDF only on Docsity!

x y

add_em

How Do Functions Work?

def add_em(x, y):

return x + y print add_em(3, 4)

function call

function definition

To evaluate a function call expression:

  1. Create a frame for the call
  2. Assign arguments to parameters
  3. Execute function body
  4. Erase the frame

The value of the function 

call expression is the returned value

arguments

parameters

function name

parameters

call frame

Return value: 7

Return value: 7

x y

sum

add_em: 1

How Do Functions Work?

def add_em(x, y):

1 sum = x + y

2 return sum

print add_em(3, 4)

function call

function definition

To evaluate a function call expression:

  1. Create a frame for the call
  2. Assign arguments to parameters
  3. Execute function body
  4. Erase the frame

The value of the function 

call expression is the returned value

arguments

parameters

statement numbers

function name program counter

parameters

call frame

local variables 7

2

def swap(x, y):

(^1) x = y

(^2) y = x

(^3) print 'x, y:', x, y

a = 1

b = 2

swap(a, b)

function call function definition

x y

swap: 1

To evaluate a function call expression:

  1. Create a frame for the call
  2. Assign arguments to parameters
  3. Execute function body
  4. Erase the frame

The value of the function 

call expression is the returned value

arguments

parameters

program counter

parameters

a 1

b 2

module (global)

variables

def swap(x, y):

1 t = x

2 x = y

3 y = t

4 print 'x, y:', x, y

a = 1

b = 2

swap(a, b)

print 'a, b:', a, b

function call function definition

x y

t

swap: 1

To evaluate a function call expression:

  1. Create a frame for the call
  2. Assign arguments to parameters
  3. Execute function body
  4. Erase the frame

The value of the function 

call expression is the returned value

arguments

parameters

program counter

parameters

local variables

a 1

b 2

module (global)

variables

def swap_x(p, q):

(^1) t = p.x

(^2) p.x = q.x

(^3) q.x = 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

function call function definition

p q

t

swap_x: 1

p id

q id

module (global)

variables

Point

x 1.

y 2.

z 3.

id

Point

x 3.

y 4.

z 5.

id

def f(x, y):

1 return 3*x + y

c = 2

print g(3)

function call function definitions

a

b

g: 1

def g(a):

1 b = f(a, c)

2 return f(b, a)

c 2

x y

f: 1

parameter

local variable

  1. Create a frame for the call
  2. Assign arguments

to parameters

  1. Execute function body
  2. Erase the frame

(9:05 version)

How to Draw Things

⟨ parameters ⟩

⟨ local variables ⟩

⟨ function name ⟩:⟨ program counter ⟩ ⟨ module name ⟩

⟨ class name ⟩

⟨ attributes ⟩

⟨ identifier ⟩

⟨ methods ⟩

⟨ class name ⟩

⟨ variable name ⟩ ⟨ old value ⟩ ⟨ value ⟩

object

class

variable

call frame

Online Python Tutor

pythontutor.com

module (global)

variables

frame for call of g

frame for call of f

output from print

goes here

controls for

stepping

through code

type in whatever

code you want