Understanding Functions in Python: Definition, Call, Parameters, and Return Statements, Schemes and Mind Maps of Anatomy

An introduction to functions in python, explaining their definition, call, parameters, and return statements. It covers the anatomy of a function definition, including the function header, docstring, and function body. The document also includes examples and exercises to help readers understand the concepts.

Typology: Schemes and Mind Maps

2021/2022

Uploaded on 08/01/2022

hal_s95
hal_s95 šŸ‡µšŸ‡­

4.4

(655)

10K documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
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
Function
Header Function
Body
(indented)
Anatomy of a Function Definition
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
name parameters
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
The return Statement
•Format:return <
expression
>
§Used to e valuate function call (as an expression)
§Also sto ps executing the function!
§Any state ments after a return ar e ignored
•Example: temperature converter function
def to_centigrade(x):
"""Returns: x converted to centigrade"""
return 5*(x-32)/9.0
A More Complex Example
Function Defini tion
def foo(a,b):
"""Return something
Param a: number
Param b: number"""
x = a
y = b
return x*y+y
Function Call
>>> x = 2
>>> foo(3,4) ?x
What is in the box?
A: 2
B: 3
C: 16
D: Nothing!
E: I do not know
•Number of statement in the
function body to execute next
•Starts with 1
Draw parameters
as variables
(named boxes)
Understanding How Functions Work
•Function Frame: Representation of function call
•A conceptual model of Python
function name
local variables (later in lecture)
parameters
instruction counter
Text (Section 3.10) vs. Class
Textbook This Clas s
def to_centigrade(x):
return 5*(x-32)/9.0
Call: to_centigrade(50.0)
Definiti on:
to_centigrade 1
50.0
x
to_centigrade x –> 50.0
pf2

Partial preview of the text

Download Understanding Functions in Python: Definition, Call, Parameters, and Return Statements and more Schemes and Mind Maps Anatomy in PDF only on Docsity!

We Write Programs to Do Things

• Functions are the key doers

Function Call Function Definition

  • Command to do the function

>>> plus( 23 )

  • Defines what function does

def plus(n):

return n+

  • 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

Function

Header Function

Body

(indented)

Anatomy of a Function Definition

def plus(n):

"""Returns the number n+

Parameter n: number to add to

Precondition: n is a number"""

x = n+

return x

Function Header

name parameters

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

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.

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

Function Call

>>> x = 2

>>> foo(3,4)

x?

What is in the box?

A: 2

B: 3

C: 16

D: Nothing!

E: I do not know

  • Number of statement in the

function body to execute next

  • Starts with 1

Draw parameters

as variables

(named boxes)

Understanding How Functions Work

• Function Frame : Representation of function call

• A conceptual model of Python

function name

local variables (later in lecture)

parameters

instruction counter

Text (Section 3.10) vs. Class

Textbook This Class

def to_centigrade(x):

return 5*(x-32)/9.

Definition : Call : to_centigrade(50.0)

to_centigrade 1

x 50.

to_centigrade

x – > 50.

Example: to_centigrade(50.0)

  1. Draw a frame for the call
  2. Assign the argument value

to the parameter (in frame)

  1. Execute the function body

§ Look for variables in the frame

§ If not there, look for global

variables with that name

  1. Erase the frame for the call

def to_centigrade(x):

return 5*(x-32)/9.

to_centigrade 1

x

Initial call frame

(before exec body)

next line to execute

Example: to_centigrade(50.0)

  1. Draw a frame for the call
  2. Assign the argument value

to the parameter (in frame)

  1. Execute the function body

§ Look for variables in the frame

§ If not there, look for global

variables with that name

  1. Erase the frame for the call

def to_centigrade(x):

return 5*(x-32)/9.

to_centigrade

x

Executing the

return statement

Return statement creates a

1 special variable for result

RETURN

Call Frames vs. Global Variables

The specification is a lie :

def swap(a,b):

"""Swap global a & b"""

tmp = a

a = b

b = tmp

>>> a = 1

>>> b = 2

>>> swap(a,b)

a 1 b 2

swap

a 1 b 2

Global Variables

Call Frame

tmp 1

x x

Global Space

(for globals.py)

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

get_a 1

a 4

globals.py

"""Show how globals work"""

a = 4 # global space

def get_a():

return a # returns global

Global Space

(for globals.py)

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

change_a

a 3.

a 4

globals.py

"""Show how globals work"""

a = 4 # global space

def change_a():

a = 3.5 # local variable

return a

Exercise Time

Function Definition

def foo(a,b):

"""Return something

Param x: a number

Param y: a number"""

x = a

y = b

return x*y+y

Function Call

>>> x = foo(3,4)

What does the

frame look like

at the start?