You are on page 1of 119

Functions and

branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Functions and branching
Beyond math functions
Foundation of programming (CK0030)
Multiple returns
Summation
No returns
Keyword arguments
Doc strings Francesco Corona
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
FdP
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
- Intro to variables, objects, modules, and text formatting
Multiple arguments
Function argument v global
- Programming with WHILE- and FOR-loops, and lists
variable
Beyond math functions
/ Functions and IF-ELSE tests
Multiple returns
Summation
No returns
Keyword arguments
/ Data reading and writing
Doc strings
/ Error handling
Functions as arguments to
functions
The main program
/ Making modules
Lambda functions

Branching
IF-ELSE blocks / Arrays and array computing
Inline IF-tests
/ Plotting curves and surfaces
Functions and
branching
FdP (cont)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns Two fundamental and extremely useful programming concepts
Summation
No returns Functions, defined by the user
Keyword arguments
Doc strings Branching, of program flow
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Summation Functions
No returns
Functions and branching
Keyword arguments
Doc strings
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Functions
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as The term function has a wider meaning than a mathematical function
Python functions
Program flow
Local and global variables
Multiple arguments Definition
Function argument v global
variable
Beyond math functions
A function is a collection of statements that can be
Multiple returns run wherever and whenever needed in the program
Summation
No returns
Keyword arguments
The function may accept input variables and may return new objects
Doc strings
Functions as arguments to
functions
To influence what is computed by the statements in it
The main program
Lambda functions

Branching
IF-ELSE blocks Functions help avoid duplicating bits of code (puts them together)
Inline IF-tests
A strategy that saves typing and makes it easier to modify code

Functions are also used to split a long program into smaller pieces
Functions and
branching
Functions (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Python comes with pre-defined functions (math.sqrt, range, len, ...)
Summation
No returns
Keyword arguments
We discuss how to define own functions
Doc strings
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Mathematical functions
Summation
No returns
Keyword arguments
as Python functions
Doc strings Functions
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Mathematical functions as Python functions
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions We want to make a Python function that evaluates a math function
Program flow
Local and global variables
Multiple arguments
Example
Function argument v global
variable
Beyond math functions Function F (C ) for converting degree Celsius C to Fahrenheit F
Multiple returns
Summation
9
No returns
F (C ) = C + 32
Keyword arguments 5
Doc strings
Functions as arguments to
functions
The main program
Lambda functions
The function (F) takes C (C ) as its input argument
Branching
IF-ELSE blocks 1 def F ( C ) :
Inline IF-tests 2 return (9.0/5) * C + 32

It returns value (9.0/5)*C + 32 (F (C )) as output


Functions and
branching
Mathematical functions as Python functions (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Definition
Function argument v global
variable
All Python functions begin with def, followed by the function name
Beyond math functions
Multiple returns
Summation Inside parentheses, a comma-separated list of function arguments
No returns
Keyword arguments
The argument acts as a standard variable inside the function
Doc strings
Functions as arguments to
functions The statements to be performed inside the function must be indented
The main program
Lambda functions

Branching
After the function it is common (not always) to return a value
IF-ELSE blocks
The function output value is sent out of the function
Inline IF-tests
Functions and
branching
Mathematical functions as Python functions (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Example
Program flow
Local and global variables
Here function name is F (F ), with only one input argument C (C )
Multiple arguments
Function argument v global
variable
1 def F ( C ) :
Beyond math functions 2 return (9.0/5) * C + 32
Multiple returns
Summation
No returns The return value is evaluated (9.0/5)*C + 32 (has no name)
Keyword arguments
Doc strings
Functions as arguments to
9
functions F (C ) = C + 32
The main program
5
Lambda functions

Branching
The returned value is the evaluation of F (C ) (implicitly F(C))
IF-ELSE blocks
Inline IF-tests

Remark
The return often (not necessarily) associates with the function name
Functions and
branching
Mathematical functions as Python functions (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
Definition
variable
Beyond math functions
The def line (function name and arguments) is the function header
Multiple returns
The indented statements are the function body
Summation
No returns
Keyword arguments
Doc strings
Functions as arguments to
functions
Example
The main program
Lambda functions 1 def F ( C ) : # Function header
Branching 2 return (9.0/5) * C + 32 # Function ( mini ) block
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Mathematical functions as Python functions (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable Definition
Beyond math functions
Multiple returns To use a function, we must call or invoke it with input arguments
Summation
No returns
Keyword arguments
Doc strings The function will process the input arguments
Functions as arguments to
functions
As a result, it will return an output value
The main program
Lambda functions
We need to store this value in a variable
Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Mathematical functions as Python functions (cont.)
UFC/DC
FdP - 2017.1
Example
Functions
Mathematical functions as
Python functions 1 # #######################################
Program flow 2 def F ( C ) : # T c o n v e r s i o n function
Local and global variables 3 return (9.0/5) * C + 32 # F(C)
Multiple arguments 4 # #######################################
Function argument v global
variable
Beyond math functions The value returned from F(C) is an object, specifically a float object
Multiple returns
Summation
No returns The call F(C) can be placed anywhere in
Keyword arguments
Doc strings
a code where a float would be valid
Functions as arguments to
functions
The main program
Lambda functions
1 temp1 = F (15.5) # Store return value as variable ( temp1 )
Branching 2
IF-ELSE blocks 3 a = 10 # Given input argument a ( value 10)
Inline IF-tests 4 temp2 = F ( a ) # Store return value as variable ( temp2 )
5
6 # Given input argument a +1 ( value 10 + 1)
7 print F ( a +1) # Print return value to screen ( no storing )
8
9 sum_temp = F (10) + F (20) # Two calls to get two output values
10 # Combines output values and store
Functions and
branching
Mathematical functions as Python functions (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Example
Function argument v global
variable Given a list Cdegrees of degrees Celsius, we want to compute a list of
Beyond math functions
Multiple returns
corresponding Fahrenheits using the F function in a list comprehension
Summation
No returns
1 # #######################################
Keyword arguments
2 def F ( C ) : # T c o n v e r s i o n function
Doc strings
Functions as arguments to
3 return (9.0/5) * C + 32 # F(C)
functions 4 # #######################################
The main program 5
Lambda functions 6 Cdegrees = [ -20 , -15 , -10 , -5 , 0 , 5 , 10 , 15 , 20 , 25 , 30 , 35]
Branching 7
IF-ELSE blocks 8 Fdegrees = [ F ( C ) for C in Cdegrees ]
Inline IF-tests
Functions and
branching
Mathematical functions as Python functions (cont.)
UFC/DC
FdP - 2017.1
Example
Functions
Mathematical functions as
Python functions
A slight variation of the F(C) function, named F2(C), can be
Program flow defined to return a formatted string instead of a real number
Local and global variables
Multiple arguments
Function argument v global 1 # #############################################################
variable 2 def F2 ( C ) : #
Beyond math functions
3 F_value = (9.0/5) * C + 32 #
Multiple returns
4 return %.1 f degrees Celsius c o r r e s p o n d to \ #
Summation
No returns
5 %.1 f degrees F a h r e n h e i t % (C , F_value ) #
Keyword arguments
6 # #############################################################
Doc strings
Functions as arguments to
functions
1 >>> s1 = F2 (21)
The main program 2 >>> print s1
Lambda functions 3 21.0 degrees Celsius c o r r e s p o n d to 69.8 F a h r e n h e i t s
Branching
IF-ELSE blocks
Inline IF-tests

Remark
Note the F_value assignment inside the function
We can create variables inside a function
We can perform operations with them
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Summation Program flow
No returns
Keyword arguments
Functions
Doc strings
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Program flow
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable Programmers must understand the sequence of statements in a program
Beyond math functions
Multiple returns There are excellent tools that help build such understanding
Summation
No returns A debugger and/or the Online Python Tutor
Keyword arguments
Doc strings
Functions as arguments to
functions
A debugger should be used for all sorts of programs, large and small
The main program
Online Python Tutor is an educational tool for small programs
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Program flow (cont.)
UFC/DC
FdP - 2017.1
Example
Functions
Mathematical functions as
Python functions
Program c2f.py contains a function F(C) and a while loop
Program flow
Local and global variables
Print a table of converted degrees Fahrenheit
Multiple arguments
Function argument v global 1 def F ( C ) :
variable
2 F = 9./5* C + 32
Beyond math functions
3 return F
Multiple returns
4
Summation
No returns
5 dC = 10
Keyword arguments
6 C = -30
Doc strings 7 while C <= 50:
Functions as arguments to 8 print %5.1 f %5.1 f % (C , F ( C ) )
functions 9 C += dC
The main program
Lambda functions

Branching A visual explanation of how the program is executed


IF-ELSE blocks
Inline IF-tests Go to Online Python Tutor (link/click me)
Forward button to advance, one statement at a time
Observe the sequence of operations
Observe the evolution of variables
Observe, observe, observe, ...
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Summation Local and global variables
No returns
Keyword arguments
Functions
Doc strings
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Local and global variables
UFC/DC
FdP - 2017.1

Functions
Definition
Mathematical functions as
Python functions
Local variables are variables that are defined within a function
Program flow
Local and global variables
Multiple arguments
Remark
Function argument v global
variable
Local variables are invisible outside functions
Beyond math functions
Multiple returns
Summation
No returns
Keyword arguments Example
Doc strings
Functions as arguments to
functions 1 >>> def F2 ( C ) :
The main program 2 ... F_value = (9.0/5) * C + 32
Lambda functions
3 ... return %.1 f degrees Celsius c o r r e s p o n d to \
Branching 4 ... %.1 f degrees F a h r e n h e i t % (C , F_value )
IF-ELSE blocks 5
Inline IF-tests 6 >>> s1 = F2 (21)
7 >>> s1
8 21.0 degrees Celsius c o r r e s p o n d to 69.8 F a h r e n h e i t s

In function F2(C), variable F_value is a local variable (inside a


function), and a local variable does not exist outside the function
Functions and
branching
Local and global variables (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Example
Local and global variables
Multiple arguments
Function argument v global
An error message shows how the (main) program around
variable (outside) function F2(C) is not aware of variable F_value
Beyond math functions
Multiple returns
Summation 1 >>> def F2 ( C ) :
No returns 2 ... F_value = (9.0/5) * C + 32
Keyword arguments
3 ... return %.1 f degrees Celsius c o r r e s p o n d to \
Doc strings
4 ... %.1 f degrees F a h r e n h e i t % (C , F_value )
Functions as arguments to
functions 5
The main program 6 >>> c1 = 37.5
Lambda functions 7 >>> s2 = F2 ( c1 )
8
Branching
IF-ELSE blocks
9 >>> F_value
Inline IF-tests 10 ...
11 N a m e E r r o r: name F_value is not defined
Functions and
branching
Local and global variables (cont.)
UFC/DC
FdP - 2017.1

Functions
Remark
Mathematical functions as
Python functions
Local variables are created inside a function
Program flow
Local and global variables
They are destroyed when leaving the function
Multiple arguments
Function argument v global
variable Also input arguments are local variables
Beyond math functions
Multiple returns
They cannot be accessed outside the function
Summation
No returns
Keyword arguments
Doc strings
Functions as arguments to
functions
Example
The main program
Lambda functions The input argument to function F2, C, is a local variable
Branching We cannot access it outside the function
IF-ELSE blocks
Inline IF-tests
1 ...
2 ...
3
4 >>> C
5 ...
6 N a m e E r r o r: name C is not defined
Functions and
branching
Local and global variables (cont
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Definition
Summation Variables defined outside the function are global variables
No returns
Keyword arguments
Doc strings
Global variables are accessible everywhere in a program
Functions as arguments to
functions Also inside a function
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Local and global variables (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables 1 # #############################################################
Multiple arguments 2 def F2 ( C ) : #
Function argument v global 3 F_value = (9.0/5) * C + 32 #
variable
Beyond math functions
4 return %.1 f degrees Celsius c o r r e s p o n d to \ #
Multiple returns
5 %.1 f degrees F a h r e n h e i t % (C , F_value ) #
Summation 6 # #############################################################
No returns
Keyword arguments
Doc strings
C and F_value are local variables
Functions as arguments to
functions
The main program
Lambda functions
1 >>> c1 = 37.5
2 >>> s2 = F2 ( c1 )
Branching
IF-ELSE blocks
Inline IF-tests c1 and s2 (and s1) are global variables
Functions and
branching
Local and global variables (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable 1 >>> F_value
Beyond math functions 2 ...
Multiple returns 3 N a m e E r r o r: name F_value is not defined
Summation
4
No returns
5 >>> C
Keyword arguments
6 ...
Doc strings
Functions as arguments to
7 N a m e E r r o r: name C is not defined
functions
The main program
Lambda functions Local variables cannot be accessed outside the function
Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Local and global variables (cont.)
UFC/DC
FdP - 2017.1

Functions Example
Mathematical functions as
Python functions
Program flow 1 # #############################################################
Local and global variables 2 def F3 ( C ) : #
Multiple arguments 3 F_value = (9.0/5) * C + 32 #
Function argument v global 4 print In F3 : C =% s F_value =% s r =% s % (C , F_value , r ) #
variable
5 return %.1 f Celsius c o r r e s p o n d to \ #
Beyond math functions
6 %.1 f F a h r e n h e i t % (C , F_value ) #
Multiple returns
Summation
7 # #############################################################
No returns
Keyword arguments Write out F_value, C, and a global variable r inside the function
Doc strings
Functions as arguments to
functions 1 >>> C = 60 # Make a global variable , C
The main program 2 >>> r = 21 # Another global variable , r
Lambda functions
3
Branching 4 >>> s3 = F3 ( r )
IF-ELSE blocks 5 In F3 : C =21 F_value =69.8 r =21
Inline IF-tests 6
7 >>> s3
8 21.0 Celsius c o r r e s p o n d to 69.8 F a h r e n h e i t
9
10 >>> C
11 60
Functions and
branching
Local and global variables (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
The example illustrates also that there are two different variables C
Program flow
Local and global variables
Multiple arguments Example
Function argument v global
variable
Beyond math functions
One local variable, existing only when the program flow is inside F3
Multiple returns
1 # #############################################################
Summation
No returns
2 def F3 ( C ) : #
Keyword arguments
3 F_value = (9.0/5) * C + 32 #
Doc strings 4 print In F3 : C =% s F_value =% s r =% s % (C , F_value , r ) #
Functions as arguments to 5 return %.1 f Celsius c o r r e s p o n d to \ #
functions 6 %.1 f F a h r e n h e i t % (C , F_value ) #
The main program
7 # #############################################################
Lambda functions

Branching
IF-ELSE blocks One global variable, defined in the main (an int object), value 60
Inline IF-tests
1 >>> C = 60
2 >>> r = 21
Functions and
branching
Local and global variables (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Example
Program flow
Local and global variables
Multiple arguments 1 # #############################################################
Function argument v global 2 def F3 ( C ) : #
variable
3 F_value = (9.0/5) * C + 32 #
Beyond math functions
4 print In F3 : C =% s F_value =% s r =% s % (C , F_value , r ) #
Multiple returns
Summation
5 return %.1 f Celsius c o r r e s p o n d to \ #
No returns
6 %.1 f F a h r e n h e i t % (C , F_value ) #
Keyword arguments
7 # #############################################################
Doc strings
Functions as arguments to
functions 1 >>> C = 60
The main program 2 >>> r = 21
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests The value of the latter (local) C is given in the call to function F3
When we refer to C in F3, we access the local variable
Inside F3, local variable C shades global variable C
Functions and
branching
Local and global variables (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Remark
Function argument v global
variable
Local variables hide global variables
Beyond math functions
Multiple returns
Summation
No returns
Keyword arguments
Doc strings
Functions as arguments to
Remark
functions
The main program
Technically, global variable C can be accessed as globals()[C]
Lambda functions

Branching
This practice is deprecated, one should avoid working with local
IF-ELSE blocks and global variables with the same names at the same time!
Inline IF-tests
Functions and
branching
Local and global variables (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
The general rule, when there are variables with the same name
Multiple returns
Summation 1 Python first looks up the name among local variables
No returns
Keyword arguments 2 then among global variables
Doc strings
Functions as arguments to 3 and, then among built-in functions
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Local and global variables (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions Example
Program flow
Local and global variables
1 print sum # sum is a built - in Python function
Multiple arguments
Function argument v global
variable First line, no local variables are present, Python then searches for a
Beyond math functions
Multiple returns
global one named sum, cannot find any, checks in built-in functions
Summation
It eventually finds a built-in function with name sum
No returns
Keyword arguments Printing sum returns <built-in function sum>
Doc strings
Functions as arguments to
functions
The main program 1 sum = 500 # rebind name sum to an int object
Lambda functions 2 print sum # sum is a global variable
Branching
IF-ELSE blocks Second line binds global name sum to an int object, when accessing
Inline IF-tests sum in print statement, Python searches among global variables
(still no local variables are present) and finds the one just defined
The printout becomes 500
Functions and
branching
Local and global variables (cont.)
UFC/DC
FdP - 2017.1
Example
Functions
Mathematical functions as
Python functions 1 print sum
Program flow 2 sum = 500
Local and global variables 3 print sum
Multiple arguments 4
Function argument v global
variable
5 def myfunc ( n ) :
Beyond math functions
6 sum = n + 1
Multiple returns 7 print sum # sum is a local variable
Summation 8 return sum
No returns 9
Keyword arguments 10 sum = myfunc (2) + 1 # new value in global variable sum
Doc strings 11 print sum
Functions as arguments to
functions
The main program
Lambda functions Call myfunc(2) invokes a function where sum is a local variable
Branching print sum makes Python first search among local variables,
IF-ELSE blocks
Inline IF-tests and since sum is found there, the printout is now 3
The printout is not 500, the value of global variable sum

Value of local variable sum is returned, added to 1, to form an int


object (value 4), the object is then bound to global variable sum

Final print sum searches among global variables, finds one value 4
Functions and
branching
Local and global variables (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Remark
Summation
The values of global variables can be accessed inside functions
No returns
Keyword arguments Though their values cannot be changed
Doc strings
Functions as arguments to Unless the variable is declared as global
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Local and global variables (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as Example
Python functions
Program flow
Local and global variables 1 a = 20; b = -2.5 # global v a r i a b l e s
Multiple arguments 2
Function argument v global 3 def f1 ( x ) :
variable
4 a = 21 # this is a new local variable
Beyond math functions
5 return a * x + b
Multiple returns
6
Summation
No returns
7 print a # shows 20
Keyword arguments 8
Doc strings 9 def f2 ( x ) :
Functions as arguments to 10 global a # a is declared global
functions
11 a = 21 # the global a is changed
The main program
12 return a * x + b
Lambda functions
13
Branching 14 f1 (3) ; print a # 20 is printed
IF-ELSE blocks 15 f2 (3) ; print a # 21 is printed
Inline IF-tests

Note that within function f1, a = 21 creates a local variable a


This does not change the global variable a
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Summation Multiple arguments
No returns
Keyword arguments
Functions
Doc strings
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Multiple arguments
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions Functions F(C) and F2(C) are functions of one single variable C
Multiple returns
Summation
The functions take one input argument (C)
No returns
Keyword arguments
Doc strings
Yet, functions can have as many input arguments as needed
Functions as arguments to
functions Need to separate the input arguments by commas (,)
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Multiple arguments (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables Example
Multiple arguments
Function argument v global
variable Consider the mathematical function
Beyond math functions
Multiple returns 1
Summation y (t) = v0 t gt 2 ,
No returns 2
Keyword arguments
Doc strings g is a fixed constant and v0 is a physical parameter that can vary
Functions as arguments to
functions
The main program Mathematically, y is a function of one variable, t
Lambda functions

Branching The function values also depend on the value v0


IF-ELSE blocks
Inline IF-tests
To evaluate y , we need values for both t and v0
Functions and
branching
Multiple arguments (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow 1.4
Local and global variables
Multiple arguments
Function argument v global
1.2
variable
Beyond math functions
1
Multiple returns
Summation
No returns 0.8
Keyword arguments
Doc strings
Functions as arguments to
0.6
functions
The main program
Lambda functions
0.4
Branching
IF-ELSE blocks
0.2
Inline IF-tests

0
0 0.5 1 1.5
Functions and
branching
Multiple arguments (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow 0.06
Local and global variables
Multiple arguments
Function argument v global 0.05
variable
Beyond math functions
Multiple returns
0.04
Summation
No returns
Keyword arguments
0.03
Doc strings
Functions as arguments to
functions
The main program
0.02
Lambda functions

Branching 0.01
IF-ELSE blocks
Inline IF-tests

0
0 0.5 1 1.5
Functions and
branching
Multiple arguments (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions A natural implementation would be a function with two arguments
Multiple returns
Summation 1 def yfunc (t , v0 ) :
No returns 2 g = 9.81
Keyword arguments
3 return v0 * t - 0.5* g * t **2
Doc strings
Functions as arguments to
functions Within the function, arguments t and v0 are local variables
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Multiple arguments (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions Example
Program flow
Local and global variables 1
Multiple arguments We want to evaluate y (t) = v0 t gt 2 for v0 = 6 [ms1 ] at t = 0.1 [s]
Function argument v global
2
variable
Beyond math functions
1 # #############################################################
Multiple returns 2 def yfunc (t , v0 ) : #
Summation 3 g = 9.81 #
No returns 4 return v0 * t - 0.5* g * t **2 #
Keyword arguments 5 # #############################################################
Doc strings 6
Functions as arguments to 7
functions
The main program
8 y = yfunc (0.1 , 6) # value1 , value2
Lambda functions 9 y = yfunc (0.1 , v0 =6) # value1 , a r g u m e n t 2= value2
10 y = yfunc ( t =0.1 , v0 =6) # a r g u m e n t 1= value1 , a r g u m e n t 2= value2
Branching
11 y = yfunc ( v0 =6 , t =0.1) # a r g u m e n t 2= value2 , a r g u m e n t 1= value1
IF-ELSE blocks
Inline IF-tests

The possibility to write argument=value in the call


facilitates reading and understanding the statement
Functions and
branching
Multiple arguments (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
With the argument=value syntax for all arguments, the sequence
Function argument v global
variable
of the arguments is no longer important (we may put v0 before t)
Beyond math functions When omitting the argument= part, the sequence of arguments
Multiple returns
Summation in the call must match the sequence of arguments in the header
No returns
Keyword arguments
Doc strings
Functions as arguments to
functions
Remark
The main program argument=value arguments must appear after
Lambda functions
all the arguments where only value is provided
Branching
IF-ELSE blocks yfunc(t=0.1, 6) is illegal
Inline IF-tests
Functions and
branching
Multiple arguments (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow Whether yfunc(0.1, 6) or yfunc(v0=6, t=0.1) is used, arguments
Local and global variables
are automatically initialised as local variables within the function
Multiple arguments
Function argument v global
variable
Beyond math functions
Initialisation is the same as assigning values to variables
Multiple returns
Summation
1 t = 0.1
No returns 2 v0 = 6
Keyword arguments 3
Doc strings 4 # #########################################################
Functions as arguments to 5 def yfunc (t , v0 ) : #
functions
6 g = 9.81 #
The main program
Lambda functions
7 return v0 * t - 0.5* g * t **2 #
8 # #########################################################
Branching
IF-ELSE blocks
Inline IF-tests Such statements are not visible in the code, yet a call to
a function automatically initialises arguments this way
Functions and
branching
Multiple arguments (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow 2
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions 1.5
Multiple returns
Summation
No returns
Keyword arguments
1
Doc strings
Functions as arguments to
functions
The main program
Lambda functions 0.5
Branching
IF-ELSE blocks
Inline IF-tests

0
0 0.5 1 1.5
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments

Function argument
Function argument v global
variable
Beyond math functions
Multiple returns
Summation
No returns
v
Keyword arguments
Doc strings
Functions as arguments to
global variable
functions
The main program
Functions
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Function argument v global variable
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
1
Local and global variables
y (t) = v0 t gt 2
Multiple arguments 2
Function argument v global
variable Mathematically, y is a function of one variable, t, the implementation
Beyond math functions
Multiple returns
as Python function, yfunc, should also be a function of t only
Summation
No returns
Keyword arguments Example
Doc strings
Functions as arguments to
functions 1 def yfunc ( t ) :
The main program 2 g = 9.81
Lambda functions 3 return v0 * t - 0.5* g * t **2
Branching
IF-ELSE blocks
Inline IF-tests
v0 becomes a global variable, which needs be initialised
outside function yfunc, before we attempt to call yfunc
Functions and
branching
Function argument v global variable (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions Failing to initialise a global variable leads to an error message
Program flow
Local and global variables
Multiple arguments
Example
Function argument v global
variable
Beyond math functions 1 >>> def yfunc ( t ) :
Multiple returns
2 ... g = 9.81
Summation
3 ... return v0 * t - 0.5* g * t **2
No returns
4
Keyword arguments
Doc strings
5 >>> yfunc (0.6)
Functions as arguments to
6 ...
functions 7 N a m e E r r o r: global name v0 is not defined
The main program
Lambda functions

Branching We need to define v0 as a global variable prior to calling yfunc


IF-ELSE blocks
Inline IF-tests 1 >>> v0 = 5
2 >>> yfunc (0.6)
3 1.2342
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Summation Beyond math functions
No returns
Keyword arguments
Functions
Doc strings
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Beyond math functions
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
So far, Python functions have typically computed some mathematical
Local and global variables expression, but their usefulness goes beyond mathematical functions
Multiple arguments
Function argument v global Any set of statements to be repeatedly executed under slightly
variable
Beyond math functions different circumstances is a candidate for a Python function
Multiple returns
Summation
No returns
Keyword arguments
Doc strings
Example
Functions as arguments to
functions We want to make a list of numbers, starting from some value (start)
The main program
Lambda functions
and stopping at some other value (stop), with given increments (inc)
Branching
IF-ELSE blocks Using variables start=2, stop=8, and inc=2,
Inline IF-tests
we would produce numbers 2, 4, 6, and 8
Functions and
branching
Beyond math functions (cont.)
UFC/DC
FdP - 2017.1

Functions
Example
Mathematical functions as
Python functions
1 def makelist ( start , stop , inc ) :
Program flow
2 value = start
Local and global variables
3 result = []
Multiple arguments
4
Function argument v global
variable 5 while value <= stop :
Beyond math functions 6 result . append ( value )
Multiple returns 7 value = value + inc
Summation 8
No returns
9 return result
Keyword arguments
10
Doc strings
Functions as arguments to
11 mylist = makelist (0 , 100 , 0.2)
functions 12 print mylist # will print 0 , 0.2 , 0.4 , 0.6 , ... 99.8 , 100
The main program
Lambda functions

Branching
IF-ELSE blocks
Function makelist has three arguments: start, stop, and inc
Inline IF-tests
Inside the function, the arguments become local variables
Also value and result are local variables

In the surrounding program (main), we define one variable, mylist


Variable mylist is a global variable
Functions and
branching
Beyond math functions (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions Remark
Multiple returns
Summation range(start, stop, inc) does not make our makelist redundant
No returns
Keyword arguments
Doc strings range can only generate integers
Functions as arguments to
functions makelist can generate real numbers, too
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Summation Multiple returns
No returns
Keyword arguments
Functions
Doc strings
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Multiple returns
UFC/DC
FdP - 2017.1
Example
Functions
Mathematical functions as
Suppose we are interested in some function y (t) and its derivative y (t)
Python functions
Program flow
1
Local and global variables
y (t) = v0 t gt 2
Multiple arguments 2
Function argument v global
variable y (t) = v0 gt
Beyond math functions
Multiple returns
Summation To get both y (t) and y (t) from same function yfunc, we include
No returns
Keyword arguments
both calculations and we separate variables in the return statement
Doc strings
Functions as arguments to
functions 1 def yfunc (t , v0 ) :
The main program 2 g = 9.81
Lambda functions 3 y = v0 * t - 0.5* g * t **2
Branching 4 dydt = v0 - g * t
IF-ELSE blocks 5 return y , dydt
Inline IF-tests

In the main, yfunc needs two names on LHS of the assignment


operator
Intuitively, as the function now returns two values

1 position , velocity = yfunc (0.6 , 3)


Functions and
branching
Multiple returns (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow 6
Local and global variables
Multiple arguments
Function argument v global 4
variable
Beyond math functions
Multiple returns
2
Summation
No returns
Keyword arguments
0
Doc strings
Functions as arguments to
functions
The main program
-2
Lambda functions

Branching -4
IF-ELSE blocks
Inline IF-tests

0 0.5 1 1.5
Functions and
branching
Multiple returns (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Example
Local and global variables
Multiple arguments
yfunc in the production of a formatted table of t , y (t) and y (t) values
Function argument v global
variable
Beyond math functions 1 t_values = [0.05* i for i in range (10) ]
Multiple returns 2
Summation 3 for t in t_values :
No returns 4 position , velocity = yfunc (t , v0 =5)
Keyword arguments 5 print t =% -10 g position =% -10 g velocity =% -10 g % \
Doc strings
6 (t , position , velocity )
Functions as arguments to
functions
The main program
Lambda functions Format %-10g prints a real number as compactly as possible (whether
Branching in decimal or scientific notation), within a field of width 10 characters
IF-ELSE blocks
Inline IF-tests The minus sign (-) after the percentage sign (%)
leads to a number is left-adjusted in this field
Important for creating nice-looking columns
Functions and
branching
Multiple returns (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable 1 t =0 position =0 velocity =5
Beyond math functions 2 t =0.05 position = 0 . 2 3 7 7 3 7 velocity =4.5095
Multiple returns 3 t =0.1 position =0.45095 velocity =4.019
Summation 4 t =0.15 position = 0 . 6 3 9 6 3 8 velocity =3.5285
No returns 5 t =0.2 position =0.8038 velocity =3.038
Keyword arguments 6 t =0.25 position = 0 . 9 4 3 4 3 7 velocity =2.5475
Doc strings
7 t =0.3 position =1.05855 velocity =2.057
Functions as arguments to
functions
8 t =0.35 position =1.14914 velocity =1.5665
The main program 9 t =0.4 position =1.2152 velocity =1.076
Lambda functions 10 t =0.45 position =1.25674 velocity =0.5855
Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Multiple returns (cont.)
UFC/DC
FdP - 2017.1
Remark
Functions
Mathematical functions as Functions returning multiple, comma-separated, values returns a tuple
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
Example
variable
Beyond math functions
1 >>> def f ( x ) :
Multiple returns
2 ... return x , x **2 , x **4
Summation
3
No returns
Keyword arguments
4 >>> s = f (2)
Doc strings 5 >>> s
Functions as arguments to 6 (2 , 4 , 16)
functions 7
The main program
8 >>> type ( s )
Lambda functions
9 < type tuple >
Branching 10
IF-ELSE blocks 11 >>> x , x2 , x4 = f (2) # store in separate v a r i a b l e s
Inline IF-tests

Remark
Storing multiple returns in separate variables, as in the last line, is the
same as storing list- (or tuple-) elements in separate variables
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Summation Summation
No returns
Keyword arguments
Functions
Doc strings
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Summation
UFC/DC
FdP - 2017.1
Example
Functions
Suppose we are interested in creating a function to calculate the sum
Mathematical functions as
n
Python functions X 1 x i
Program flow L(x; n) =
Local and global variables
i =1
i 1+x
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Summation
No returns
3
Keyword arguments
Doc strings
Functions as arguments to
2.5
functions
The main program
Lambda functions
2
Branching
IF-ELSE blocks 1.5
Inline IF-tests

0.5

0
0 10 20 30 40 50
Functions and
branching
Summation
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
To compute the sum, a loop and add terms to an accumulation variable
Program flow
Local and global variables
We performed a similar task with a while loop
Multiple arguments
Function argument v global
variable
Beyond math functions Example
Multiple returns
Summation Summations with integer counters (like i) are normally
No returns
Keyword arguments
(often) implemented by a for-loop over the i counter
Doc strings
n
Functions as arguments to X
functions
The main program
i2
Lambda functions i =1
Branching
IF-ELSE blocks
1 s = 0
Inline IF-tests
2 for i in range (1 , n +1) :
3 s += i **2
Functions and
branching
Summation (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
n
Function argument v global X 1 x i
variable L(x; n) =
Beyond math functions
i =1
i 1+x
Multiple returns
Summation
No returns
Keyword arguments
1 s = 0
Doc strings
2 for i in range (1 , n +1) :
Functions as arguments to
3 s += (1.0/ i ) *( x /(1.0+ x ) ) ** i
functions
The main program
Lambda functions
Observe the terms 1.0 used to avoid integer division
Branching
IF-ELSE blocks
i is an int object and x may also be an int
Inline IF-tests
Functions and
branching
Summation (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow n
Local and global variables
X 1 x i
L(x; n) =
Multiple arguments
Function argument v global i =1
i 1+x
variable
Beyond math functions
Multiple returns We want to embed the computation of the sum in a Python function
Summation
No returns
Keyword arguments x and n are the input arguments
Doc strings
Functions as arguments to
functions 1 def L (x , n ) :
The main program 2 s = 0
Lambda functions 3 for i in range (1 , n +1) :
Branching 4 s += (1.0/ i ) *( x /(1.0+ x ) ) ** i
IF-ELSE blocks 5 return s
Inline IF-tests

The sum s is the return output


Functions and
branching
Summation (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
It can be shown that L(x; n) is an approximation to ln (1 + x) for a finite
Beyond math functions n and for x 1, with the approximation becoming exact in the limit
Multiple returns
Summation
No returns
lim L(x; n) = ln (1 + x)
n
Keyword arguments
Doc strings
Functions as arguments to
functions
The main program Instead of having L return only the value of the sum s, it would be also
Lambda functions
interesting to return additional information on the approximation error
Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Summation (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
n
Function argument v global X 1 x i
variable
L(x; n) =
Beyond math functions
i =1
i 1+x
Multiple returns
Summation
No returns
Keyword arguments The size of the terms decreases with n, the first neglected term
Doc strings
Functions as arguments to
(n+1) is bigger than all the remaining terms (for n+2,n+3,...),
functions
but not necessarily bigger than their sum
The main program
Lambda functions The first neglected term is hence an indication of the size of the
Branching total error, we may use this term as a rough estimate of the error
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Summation (cont.)
UFC/DC
FdP - 2017.1
We return the exact error (we calculate the log function by math.log)
Functions
Mathematical functions as
Python functions
Program flow
Local and global variables Example
Multiple arguments
Function argument v global
variable 1 # #############################################################
Beyond math functions 2 def L2 (x , n ) : #
Multiple returns 3 s = 0 #
Summation 4 for i in range (1 , n +1) : #
No returns
5 s += (1.0/ i ) *( x /(1.0+ x ) ) ** i
Keyword arguments
6 #
Doc strings
7 value_of_sum = s #
Functions as arguments to
functions 8 f i r s t _ n e g l e c t e d _ t e r m = (1.0/( n +1) ) *( x /(1.0+ x ) ) **( n +1) #
The main program 9 #
Lambda functions 10 from math import log #
Branching 11 #
IF-ELSE blocks 12 e x a c t _ e r r o r = log (1+ x ) - v a l u e _ o f _ s u m #
Inline IF-tests 13 return value_of_sum , f i r s t _ n e g l e ct ed _t er m , e x a c t _ e r r o r #
14 # #############################################################
15
16
17 # typical call :
18 value , a p p r o x i m a t e_ er ror , e x a c t _ e r r o r = L2 (x , 100)
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Summation No returns
No returns
Keyword arguments
Functions
Doc strings
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
No returns
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns Sometimes a function can be defined to performs a set of statements
Summation
No returns Without necessarily computing objects returned to calling code
Keyword arguments
Doc strings In such situations, the return statement is not needed
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
No returns (cont.)
UFC/DC
FdP - 2017.1
The example shows the concept of function without return values
Functions Example
Mathematical functions as
Python functions
Program flow A table of the accuracy of the L(x; n) approximation to ln (1 + x)
Local and global variables
Multiple arguments
Function argument v global 1 # #############################################################
variable 2 def L2 (x , n ) : #
Beyond math functions
3 s = 0 #
Multiple returns
4 for i in range (1 , n +1) : #
Summation
5 s += (1.0/ i ) *( x /(1.0+ x ) ) ** i
No returns
Keyword arguments
6 #
Doc strings
7 value_of_sum = s #
Functions as arguments to 8 f i r s t _ n e g l e c t e d _ t e r m = (1.0/( n +1) ) *( x /(1.0+ x ) ) **( n +1) #
functions 9 #
The main program 10 from math import log #
Lambda functions
11 #
Branching 12 e x a c t _ e r r o r = log (1+ x ) - v a l u e _ o f _ s u m #
IF-ELSE blocks 13 return value_of_sum , f i r s t _ n e g l e ct ed _t er m , e x a c t _ e r r o r #
Inline IF-tests 14 # #############################################################
15
16 def table ( x ) :
17 print \ nx =% g , ln (1+ x ) =% g % (x , log (1+ x ) )
18 for n in [1 , 2 , 10 , 100 , 500]:
19 value , next , error = L2 (x , n )
20 print n =% -4 d % -10 g ( next term : %8.2 e \
21 error : %8.2 e ) % (n , value , next , error )
Functions and
branching
No returns (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow 1 >>> table (10)
Local and global variables 2 x =10 , ln (1+ x ) =2.3979
Multiple arguments 3 n =1 0.909091 ( next term : 4.13 e -01 error : 1.49 e +00)
Function argument v global 4 n =2 1.32231 ( next term : 2.50 e -01 error : 1.08 e +00)
variable
Beyond math functions
5 n =10 2.17907 ( next term : 3.19 e -02 error : 2.19 e -01)
Multiple returns
6 n =100 2.39789 ( next term : 6.53 e -07 error : 6.59 e -06)
Summation 7 n =500 2.3979 ( next term : 3.65 e -24 error : 6.22 e -15)
No returns 8
Keyword arguments 9
Doc strings 10 >>> table (1000)
Functions as arguments to 11 x =1000 , ln (1+ x ) =6.90875
functions
The main program
12 n =1 0.999001 ( next term : 4.99 e -01 error : 5.91 e +00)
Lambda functions
13 n =2 1.498 ( next term : 3.32 e -01 error : 5.41 e +00)
14 n =10 2.919 ( next term : 8.99 e -02 error : 3.99 e +00)
Branching
15 n =100 5.08989 ( next term : 8.95 e -03 error : 1.82 e +00)
IF-ELSE blocks
16 n =500 6.34928 ( next term : 1.21 e -03 error : 5.59 e -01)
Inline IF-tests

Error is an order of magnitude larger than the first neglected term


Convergence is slower for larger values of x than smaller x
Functions and
branching
No returns (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions Remark
Multiple returns
Summation For functions w/o return statement, Python inserts an invisible one
No returns
Keyword arguments
The invisible return is named None
Doc strings
Functions as arguments to
None is a special object in Python that represents
functions
The main program
something we might think of as the nothingness
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
No returns (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions Example
Multiple returns
Summation
Normally, one would call function table w/o assigning return value
No returns
Keyword arguments
Doc strings Imagine we assign the return value to a variable
Functions as arguments to
functions result = table(500), the result will refer to a None object
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
No returns (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
The None value is often used for variables that should exist in a program,
Function argument v global but where it is natural to think of the value as conceptually undefined
variable
Beyond math functions
Multiple returns
Summation Remark
No returns
Keyword arguments
The standard way to test if an object obj is set to None or not reads
Doc strings
Functions as arguments to
1 if obj is None :
functions 2 ...
The main program 3
Lambda functions 4 if obj is not None :
Branching 5 ...
IF-ELSE blocks
Inline IF-tests
Functions and
branching
No returns (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
The is operator tests if two names refer to the same object
Multiple arguments
Function argument v global
variable
The == tests checks if the contents of two objects are the same
Beyond math functions
Multiple returns 1 >>> a = 1
Summation 2 >>> b = a
No returns
3 >>> a is b # a and b refer to the same object
Keyword arguments
4 True
Doc strings
5
Functions as arguments to
functions 6 >>> c = 1.0 # a and c do not refer to the same object
The main program 7 >>> a is c
Lambda functions 8 False
Branching 9
IF-ELSE blocks 10 >>> a == c # a and c are m a t h e m a t i c a l l y equal
Inline IF-tests 11 True
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Summation Keyword arguments
No returns
Keyword arguments
Functions
Doc strings
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Keyword arguments
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
The input arguments of a function can be assigned a default value
Beyond math functions These arguments can be left out in the call
Multiple returns
Summation
No returns This is how a such a function may be defined
Keyword arguments
Doc strings 1 # #############################################################
Functions as arguments to 2 def somefunc ( arg1 , arg2 , kwarg1 = True , kwarg2 =0) : #
functions
The main program
3 print arg1 , arg2 , kwarg1 , kwarg2 #
Lambda functions
4 # #############################################################
Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Keyword arguments (cont.)
UFC/DC
FdP - 2017.1
1 # #############################################################
Functions 2 def somefunc ( arg1 , arg2 , kwarg1 = True , kwarg2 =0) : #
Mathematical functions as 3 print arg1 , arg2 , kwarg1 , kwarg2 #
Python functions
4 # #############################################################
Program flow
Local and global variables
Multiple arguments
First args (here, arg1 and arg2) are ordinary/positional arguments
Function argument v global
variable
Last two args (kwarg1 and kwarg2) are keyword/named arguments
Beyond math functions
Multiple returns
Summation Each keyword argument has a name and an associated a default value
No returns
Keyword arguments
Doc strings
Functions as arguments to
Example
functions
The main program 1 >>> somefunc ( Hello , [1 ,2])
Lambda functions
2 Hello [1 , 2] True 0
Branching 3
IF-ELSE blocks 4 >>> somefunc ( Hello , [1 ,2] , kwarg1 = Hi )
Inline IF-tests 5 Hello [1 , 2] Hi 0
6
7 >>> somefunc ( Hello , [1 ,2] , kwarg2 = Hi )
8 Hello [1 , 2] True Hi
9
10 >>> somefunc ( Hello , [1 ,2] , kwarg2 = Hi , kwarg1 =6)
11 Hello [1 , 2] 6 Hi
Functions and
branching
Keyword arguments (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Remark
Beyond math functions
Multiple returns
Keyword arguments must be listed AFTER positional arguments
Summation
No returns When ALL input arguments are explicitly referred to (name=value),
Keyword arguments
Doc strings
the sequence is not relevant: positional and keyword can be mixed up
Functions as arguments to
functions
The main program
1 >>> somefunc ( kwarg2 = Hello , arg1 = Hi , kwarg1 =6 , arg2 =[1 ,2])
Lambda functions 2 Hi [1 , 2] 6 Hello
Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Keyword arguments (cont.)
UFC/DC
FdP - 2017.1

Functions Example
Mathematical functions as
Python functions
Program flow Consider some function of t also containing some parameters A, a and
Local and global variables
Multiple arguments
Function argument v global
f (t; A, a, ) = Ae at sin (t)
variable
Beyond math functions
Multiple returns
Summation
No returns
Keyword arguments
Doc strings 100
Functions as arguments to
functions
The main program
50
Lambda functions

Branching
IF-ELSE blocks 0
Inline IF-tests

-50

-5 -4 -3 -2 -1 0 1 2 3 4 5
Functions and
branching
Keyword arguments (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable f (t; A, a, ) = Ae at sin (t)
Beyond math functions
Multiple returns
Summation
No returns
We code f as function of independent variable t, ordinary argument,
Keyword arguments with parameters A, a, and as keyword arguments with default values
Doc strings
Functions as arguments to
functions 1 from math import pi , exp , sin
The main program 2
Lambda functions
3 def f (t , A =1 , a =1 , omega =2* pi ) :
Branching 4 return A * exp ( - a * t ) * sin ( omega * t )
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Keyword arguments (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables 1 # #############################################################
Multiple arguments 2 def f (t , A =1 , a =1 , omega =2* pi ) : #
Function argument v global 3 return A * exp ( - a * t ) * sin ( omega * t ) #
variable
Beyond math functions
4 # #############################################################
Multiple returns
Summation We can call function f with only argument t specified
No returns
Keyword arguments 1 v1 = f (0.2)
Doc strings
Functions as arguments to
functions
Other possible calls are listed below
The main program
Lambda functions
1 v2 = f (0.2 , omega =1)
Branching 2 v3 = f (1 , A =5 , omega = pi , a = pi **2)
IF-ELSE blocks 3 v4 = f ( A =5 , a =2 , t =0.01 , omega =0.1)
Inline IF-tests 4 v5 = f (0.2 , 0.5 , 1 , 1)
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions Example
Program flow
Local and global variables
Consider L(x; n) and functional implementations L(x,n) and L2(x,n)
Multiple arguments
Function argument v global
n
variable X 1 x i
Beyond math functions L(x; n) = , with lim L(x; n) = ln (1 + x), for x 1
Multiple returns
i =1
i 1+x n
Summation
No returns
Keyword arguments Instead of specifying the number n of terms in the sum,
Doc strings
we now specify a minimum tolerance in the accuracy
Functions as arguments to
functions
The main program
Lambda functions We can use the first neglected term as an estimate of the accuracy
Branching
IF-ELSE blocks
We add terms as long as the absolute
Inline IF-tests value of the next term is greater than
Functions and
branching
Keyword arguments (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
n
Python functions X 1 x i
Program flow
L(x; n) =
Local and global variables
i =1
i 1+x
Multiple arguments
Function argument v global
variable It is natural to provide a default value for
Beyond math functions
Multiple returns
Summation
1 # #############################################################
No returns 2 def L3 (x , epsilon =1.0 E -6) : #
Keyword arguments 3 x = float ( x ) #
Doc strings 4 i = 1 #
Functions as arguments to 5 term = (1.0/ i ) *( x /(1+ x ) ) ** i #
functions
6 s = term #
The main program
Lambda functions
7 #
8 while abs ( term ) > epsilon : #
Branching 9 i += 1 #
IF-ELSE blocks
10 term = (1.0/ i ) *( x /(1+ x ) ) ** i #
Inline IF-tests
11 s += term #
12
13 return s , i #
14 # #############################################################
Functions and
branching
Keyword arguments (cont.)
UFC/DC
FdP - 2017.1
We make a table of the approximation error as decreases
Functions
Mathematical functions as 1 # #############################################################
Python functions
2 def L3 (x , epsilon =1.0 E -6) : #
Program flow
3 x = float ( x ) #
Local and global variables
4 i = 1 #
Multiple arguments
Function argument v global
5 term = (1.0/ i ) *( x /(1+ x ) ) ** i #
variable 6 s = term #
Beyond math functions 7 #
Multiple returns 8 while abs ( term ) > epsilon : #
Summation 9 i += 1 #
No returns
10 term = (1.0/ i ) *( x /(1+ x ) ) ** i #
Keyword arguments
11 s += term #
Doc strings
12
Functions as arguments to
functions 13 return s , i #
The main program 14 # #############################################################
Lambda functions 15
Branching 16 def table2 ( x ) :
IF-ELSE blocks 17 from math import log
Inline IF-tests 18
19 for k in range (4 , 14 , 2) :
20 epsilon = 10**( -k )
21 approx , n = L3 (x , epsilon = epsilon )
22 exact = log (1+ x )
23 e x a c t _ e r r o r = exact - approx
24 ...
Functions and
branching
Keyword arguments (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables The output from calling table2(10) should look like
Multiple arguments
Function argument v global
variable 1 epsilon : 1e -04 , exact error : 8.18 e -04 , n =55
Beyond math functions 2 epsilon : 1e -06 , exact error : 9.02 e -06 , n =97
Multiple returns 3 epsilon : 1e -08 , exact error : 8.70 e -08 , n =142
Summation 4 epsilon : 1e -10 , exact error : 9.20 e -10 , n =187
No returns
5 epsilon : 1e -12 , exact error : 9.31 e -12 , n =233
Keyword arguments
Doc strings
Functions as arguments to
functions The epsilon estimate is about ten times smaller than the exact error
The main program
Lambda functions
regardless of the size of epsilon
Branching
IF-ELSE blocks Since epsilon follows the exact error over many orders of magnitude,
Inline IF-tests
we may view epsilon as a useful indication of the size of the error
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Summation Doc strings
No returns
Keyword arguments
Functions
Doc strings
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Doc strings
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
There is a convention to augment functions with some documentation
variable
Beyond math functions
The documentation string, known as a doc string, should
Multiple returns contain a short description of the purpose of the function
Summation
No returns It should explain what arguments and return values are
Keyword arguments
Doc strings Usually, right after the def funcname: line of definition
Functions as arguments to
functions
The main program Doc strings are usually enclosed in triple double quotes """"
Lambda functions

Branching
This allows the string to span several lines
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Doc strings (cont.)
UFC/DC
FdP - 2017.1
Example
Functions
Mathematical functions as
Python functions 1 def C2F ( C ) :
Program flow 2 """ Convert Celsius degrees ( C ) to F a h r e n h e i t. """
Local and global variables
3 return (9.0/5) * C + 32
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Summation Example
No returns
Keyword arguments
1 def line ( x0 , y0 , x1 , y1 ) :
Doc strings
2 """
Functions as arguments to
functions 3 Compute the c o e f f i c i e n t s a and b in the m a t h e m a t i c a l
The main program 4 e x p r e s s i o n for a straight line y = a * x + b that goes
Lambda functions 5 through two points ( x0 , y0 ) and ( x1 , y1 ) .
Branching 6
IF-ELSE blocks 7 x0 , y0 : a point on the line ( floats ) .
Inline IF-tests 8 x1 , y1 : another point on the line ( floats ) .
9 return : c o e f f i c i e n t s a , b ( floats ) for the line ( y = a * x + b ) .
10 """
11
12 a = ( y1 - y0 ) / float ( x1 - x0 )
13 b = y0 - a * x0
14 return a , b
Functions and
branching
Doc strings (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as To extract doc strings from source code use funcname.__doc__
Python functions
Program flow
Local and global variables
Multiple arguments Example
Function argument v global
variable
Beyond math functions
1 print line . __doc__
Multiple returns
Summation
1 Compute the c o e f f i c i e n t s a and b in the m a t h e m a t i c a l
No returns
2 e x p r e s s i o n for a straight line y = a * x + b that goes
Keyword arguments
Doc strings
3 through two points ( x0 , y0 ) and ( x1 , y1 ) .
4
Functions as arguments to
functions 5 x0 , y0 : a point on the line ( float objects ) .
The main program 6 x1 , y1 : another point on the line ( float objects ) .
Lambda functions 7 return : c o e f f i c i e n t s a , b ( floats ) for the line ( y = a * x + b ) .
Branching
IF-ELSE blocks
Inline IF-tests If function line is in a file funcs.py, we can run pydoc funcs.line
Shows the documentation of function line
Function signature and doc string
Functions and
branching
Doc strings (cont.)
UFC/DC
FdP - 2017.1 Doc strings often contain interactive sessions, from the Python shell
Functions Used to illustrate how the function can be used
Mathematical functions as
Python functions
Program flow Example
Local and global variables
Multiple arguments
1 def line ( x0 , y0 , x1 , y1 ) :
Function argument v global
variable 2 """
Beyond math functions 3 Compute the c o e f f i c i e n t s a and b in the m a t h e m a t i c a l
Multiple returns 4 e x p r e s s i o n for a straight line y = a * x + b that goes
Summation 5 through two points ( x0 , y0 ) and ( x1 , y1 ) .
No returns 6
Keyword arguments 7 x0 , y0 : a point on the line ( float ) .
Doc strings
8 x1 , y1 : another point on the line ( float ) .
Functions as arguments to
functions
9 return : c o e f f i c i e n t s a , b ( floats ) for the line ( y = a * x + b ) .
The main program 10
Lambda functions 11 Example :
Branching
12 >>> a , b = line (1 , -1 , 4 , 3)
IF-ELSE blocks
13 >>> a
Inline IF-tests
14 1.3333333333333333
15 >>> b
16 - 2 . 3 3 3 3 3 3 3 33 33 333 3
17 """
18
19 a = ( y1 - y0 ) / float ( x1 - x0 )
20 b = y0 - a * x0
21 return a , b
Functions and
branching
Functions (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions It is a convention in Python that function arguments represent input
Program flow
Local and global variables
data to the function, while returned objects represent output data
Multiple arguments
Function argument v global A general Python function looks like
variable
Beyond math functions
Multiple returns Definition
Summation
No returns 1 def somefunc ( i1 , i2 , i3 , io4 , io5 , i6 = value1 , io7 = value2 ) :
Keyword arguments 2 # modify io4 , io5 , io6 ; compute o1 , o2 , o3
Doc strings
3 return o1 , o2 , o3 , io4 , io5 , io7
Functions as arguments to
functions
The main program
Lambda functions
i1, i2, i3 are positional arguments, input data
Branching io4 and io5 are positional arguments, input and output data
IF-ELSE blocks
Inline IF-tests i6 and io7 are keyword arguments, input and input/output data
o1, o2, and o3 are computed in the function, output data
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Functions
Summation
No returns
Keyword arguments
as arguments to functions
Doc strings
Functions as arguments to
Functions
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Functions as arguments to functions
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables We can have functions to be used as arguments to other functions
Multiple arguments
Function argument v global
variable
Beyond math functions
A math function f (x) may be needed for specific Python functions
Multiple returns
Numerical root finding: Solve f (x) = 0, approximately
Summation
No returns Numerical differentiation: Compute f (x), approximately
Keyword arguments
Rb
Doc strings
Numerical integration: Compute a
f (x)dx, approximately
Functions as arguments to
functions
The main program dx
Lambda functions
Numerical solution of differential equations: = f (x)
dt
Branching
IF-ELSE blocks
Inline IF-tests
In such functions, function f (x) can be used as input argument (f)
Functions and
branching
Functions as arguments to functions (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
This is straightforward in Python and hardly needs any explanation
Multiple returns
Summation Remark
No returns
Keyword arguments
Doc strings
In most other languages, special constructions must be used
Functions as arguments to
functions
for transferring a function to another function as argument
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Functions as arguments to functions (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Compute the 2nd-order derivative of some function f (x), numerically
Program flow
Local and global variables
Multiple arguments f (x h) 2f (x) + f (x + h)
Function argument v global f (x) , with h a small number
variable h2
Beyond math functions
Multiple returns
Summation
No returns
Keyword arguments
Example
Doc strings
Functions as arguments to
functions A Python function for the task can be implemented as follows
The main program
Lambda functions
1 def diff2nd (f , x , h =1 E -6) :
2 r = ( f (x - h ) - 2* f ( x ) + f ( x + h ) ) / float ( h * h )
Branching
3 return r
IF-ELSE blocks
Inline IF-tests

f is, like other input arguments, a name, for a function object


Functions and
branching
Functions as arguments to functions (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables 1 # #############################################################
Multiple arguments 2 def g ( t ) : # g ( t ) = t ^( -6) #
Function argument v global 3 return t **( -6) #
variable
4 # #############################################################
Beyond math functions
5
Multiple returns
Summation
6 # #############################################################
No returns
7 def diff2nd (f , x , h =1 E -6) : #
Keyword arguments 8 r = ( f (x - h ) - 2* f ( x ) + f ( x + h ) ) / float ( h * h ) #
Doc strings 9 return r #
Functions as arguments to 10 # #############################################################
functions
11
The main program
12
Lambda functions
13 t = 1.2
Branching 14 d2g = diff2nd (g , t )
IF-ELSE blocks 15
Inline IF-tests
16 print " g (%f ) =% f " % (t , d2g )
Functions and
branching
Functions as arguments to functions (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow Asymptotically, the numerical approximation of
Local and global variables
Multiple arguments
the derivative becomes more accurate as h 0
Function argument v global
variable
Beyond math functions
Multiple returns
Example
Summation
No returns
Keyword arguments
We show this property by making a table of the second-order derivatives
Doc strings g (t) = t 6 at t = 1 as h 0
Functions as arguments to
functions
The main program
Lambda functions 1 for k in range (1 ,15):
Branching
2 h = 10**( - k )
IF-ELSE blocks
3 d2g = diff2nd (g , 1 , h )
Inline IF-tests
4 print h =%.0 e : %.5 f % (h , d2g )
Functions and
branching
Functions as arguments to functions (cont.)
UFC/DC
FdP - 2017.1
The exact answer is g (t = 1) = 42
Functions
Mathematical functions as 1 h =1 e -01: 44.61504
Python functions
Program flow
2 h =1 e -02: 42.02521
Local and global variables
3 h =1 e -03: 42.00025
Multiple arguments 4 h =1 e -04: 42.00000
Function argument v global 5 h =1 e -05: 41.99999
variable
6 h =1 e -06: 42.00074
Beyond math functions
7 h =1 e -07: 41.94423
Multiple returns
8 h =1 e -08: 47.73959
Summation
No returns
9 h =1 e -09: -666.13381
Keyword arguments
10 h =1 e -10: 0.00000
Doc strings
11 h =1 e -11: 0.00000
Functions as arguments to 12 h =1 e -12: -666133814.77509
functions 13 h =1 e -13: 66613381477.50939
The main program
14 h =1 e -14: 0.00000
Lambda functions

Branching Computations start returning very inaccurate results for h < 108
IF-ELSE blocks
Inline IF-tests For small h, on a computer, rounding errors in
the formula blow up and destroy the accuracy
Switching from standard floating-point numbers
(float) to numbers with arbitrary high precision
(module decimal) resolves the problem
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Summation The main program
No returns
Keyword arguments
Functions
Doc strings
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
The main program
UFC/DC
FdP - 2017.1

Functions In programs with functions, a part of the program is called main


Mathematical functions as
Python functions
It is the collection of all statements outside the functions
Program flow
Local and global variables Plus, the definition of all functions
Multiple arguments
Function argument v global
variable
Beyond math functions
Example
Multiple returns
Summation 1 from math import * # in main
No returns 2
Keyword arguments 3 def f ( x ) : # in main
Doc strings
4 e = exp ( -0.1* x )
Functions as arguments to
functions
5 s = sin (6* pi * x )
The main program 6 return e * s
Lambda functions 7

Branching
8 x = 2 # in main
IF-ELSE blocks
9 y = f(x) # in main
Inline IF-tests
10 print f (% g ) =% g % (x , y ) # in main

The main program here consists of the lines with comment in main
The execution always starts with the first line in the main
Functions and
branching
The main program (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables 1 from math import * # in main
Multiple arguments 2
Function argument v global 3 def f ( x ) : # in main
variable
Beyond math functions
4 e = exp ( -0.1* x )
Multiple returns
5 s = sin (6* pi * x )
Summation
6 return e * s
No returns 7
Keyword arguments 8 x = 2 # in main
Doc strings 9 y = f(x) # in main
Functions as arguments to 10 print f (% g ) =% g % (x , y ) # in main
functions
The main program
Lambda functions
When a function is encountered, its statements are used to define it
Branching
IF-ELSE blocks Nothing is computed inside a function before it is called
Inline IF-tests
Variables initialised in the main program become global variables
Functions and
branching
The main program (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as 1 from math import * # in main
Python functions
2
Program flow
Local and global variables
3 def f ( x ) : # in main
Multiple arguments 4 e = exp ( -0.1* x )
Function argument v global 5 s = sin (6* pi * x )
variable
6 return e * s
Beyond math functions
7
Multiple returns
8 x = 2 # in main
Summation
No returns
9 y = f(x) # in main
Keyword arguments
10 print f (% g ) =% g % (x , y ) # in main
Doc strings
Functions as arguments to
functions 1 Import functions from the math module
The main program
Lambda functions 2 Define function f(x)
Branching 3 Define x
IF-ELSE blocks
Inline IF-tests 4 Call f and execute the function body
5 Define y as the value returned from f
6 Print a string
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Summation Lambda functions
No returns
Keyword arguments
Functions
Doc strings
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Lambda functions
UFC/DC
FdP - 2017.1

Functions A one-line construction of functions used to make code compact


Mathematical functions as
Python functions
1 f = lambda x : x **2 + 4
Program flow
Local and global variables
Multiple arguments
Function argument v global
This so-called lambda function is equivalent to the usual form
variable
Beyond math functions 1 def f ( x ) :
Multiple returns 2 return x **2 + 4
Summation
No returns
Keyword arguments
Doc strings
Functions as arguments to
functions
Definition
The main program
Lambda functions
In general, we have
Branching 1 def g ( arg1 , arg2 , arg3 , ...) :
IF-ELSE blocks 2 return e x p r e s s i o n
Inline IF-tests

written in the form


1 g = lambda arg1 , arg2 , arg3 , ...: e x p r e s s i o n
Functions and
branching
Lambda functions (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Lambda functions are used for function argument to functions
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Example
Multiple returns
Summation
Consider the diff2nd function used to differentiate g (t) = t 6 twice
No returns
We first make a g(t) then pass g as input argument to diff2nd
Keyword arguments
Doc strings
Functions as arguments to
functions
We skip the step of defining g(t) and use a lambda function instead
The main program
Lambda functions
A lambda function f as input argument into diff2nd
Branching
IF-ELSE blocks
Inline IF-tests
1 d2 = diff2nd ( lambda t : t **( -6) , 1 , h =1 E -4)
Functions and
branching
Lambda functions (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns Remark
Summation
No returns Lambda functions can also take keyword arguments
Keyword arguments
Doc strings 1 d2 = diff2nd ( lambda t , A =1 , a =0.5: -a *2* t * A * exp ( - a * t **2) , 1.2)
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Summation Branching
No returns
Keyword arguments
Functions and branching
Doc strings
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Branching
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
The flow of computer programs often needs to branch
Python functions
Program flow if a condition is met, we
Local and global variables
do one thing;
(
Multiple arguments sin (x), 0x
Function argument v global
if it is not, we do some f (x) =
variable
0, otherwise
Beyond math functions
Multiple returns
other thing
Summation
No returns
Keyword arguments
Implementing this requires a test on the value of x
Doc strings
Functions as arguments to
functions
The main program
Example
Lambda functions

Branching
1 def f ( x ) :
IF-ELSE blocks
2 if 0 <= x <= pi :
Inline IF-tests 3 value = sin ( x )
4 else :
5 value = 0
6 return value
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Summation IF-ELSE blocks
No returns
Keyword arguments
Branching
Doc strings
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
IF-ELSE blocks
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions Definition
Program flow
Local and global variables
The general structure of the IF-ELSE test
Multiple arguments
Function argument v global
1 if c o n d i t i o n:
variable 2 < block of statements ,
Beyond math functions 3 executed if c o n d i t i o n is True >
Multiple returns 4
Summation
5 else :
No returns
6 < block of statements ,
Keyword arguments
7 executed if c o n d i t i o n is False >
Doc strings
Functions as arguments to
functions
The main program If condition is True, the program flow goes into the first
Lambda functions
block of statements, indented after the if: line
Branching
IF-ELSE blocks
If condition is False, program flow goes into the second
Inline IF-tests block of statements, indented after the else: line

The blocks of statements are indented, and note the two-points


Functions and
branching
IF-ELSE blocks (cont.)
UFC/DC
FdP - 2017.1
Example
Functions
Mathematical functions as
Python functions 1 if C < -273.15:
Program flow 2 print % g degrees Celsius is non - physical ! % C
Local and global variables
3 print The F a h r e n h e i t t e m p e r a t u r e will not be computed .
Multiple arguments
4
Function argument v global
variable 5 else :
Beyond math functions 6 F = 9.0/5* C + 32
Multiple returns 7 print F
Summation 8
No returns 9 print end of program
Keyword arguments
Doc strings
Functions as arguments to
functions
The main program
The two print statements in the IF-block are executed
Lambda functions if and only if condition C < -273.15 evaluates as True
Branching
Otherwise, the execution skips the print statements
IF-ELSE blocks
Inline IF-tests and carries out with the computation of the
statements in the ELSE-block and prints F

The end of program bit is printed regardless of the outcome


This statement is not indented and it is neither part
of the IF-block nor of the ELSE-block
Functions and
branching
IF-ELSE blocks (cont.)
UFC/DC
FdP - 2017.1

Functions The else part of the IF-ELSE test can be skipped


Mathematical functions as
Python functions
Program flow Definition
Local and global variables
Multiple arguments 1 if c o n d i t i o n:
Function argument v global 2 < block of statements >
variable
Beyond math functions
3 < next statement >
Multiple returns
Summation
No returns
Keyword arguments
Doc strings
Functions as arguments to Example
functions
The main program
Lambda functions 1 if C < -273.15:
2 print % s degrees Celsius is non - physical ! % C
Branching
3 F = 9.0/5* C + 32
IF-ELSE blocks
Inline IF-tests
The computation of F will always be carried out
The statement is not indented
It is not a part of the IF-block
Functions and
branching
IF-ELSE blocks (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow Definition
Local and global variables
Multiple arguments
With elif (for else if) several mutually exclusive IF-test are performed
Function argument v global
variable 1 if c o n d i t i o n 1:
Beyond math functions 2 < block of statements >
Multiple returns 3
Summation 4 elif c o n d i t i o n 2:
No returns
5 < block of statements >
Keyword arguments
6
Doc strings
7 elif c o n d i t i o n 3:
Functions as arguments to
functions 8 < block of statements >
The main program 9
Lambda functions 10 else :
Branching 11 < block of statements >
IF-ELSE blocks 12 < next statement >
Inline IF-tests

This construct allows for multiple branching of the program flow


Functions and
branching
IF-ELSE blocks (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
Example
variable
Beyond math functions Let us consider the so-called HAT function
Multiple returns

Summation
No returns

0, x <0

Keyword arguments x, 0x<1
Doc strings N(x) =
2 x, 1x2
Functions as arguments to
functions

0, x 2

The main program
Lambda functions

Branching
Define a Python function that codes it
IF-ELSE blocks
Inline IF-tests
Functions and
branching
IF-ELSE blocks (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow

Local and global variables

0, x <0

Multiple arguments x, 0x<1
Function argument v global N(x) =
variable
Beyond math functions


2 x, 1x2
0, x 2

Multiple returns
Summation
No returns
Keyword arguments
1 def N ( x ) :
Doc strings
Functions as arguments to
2 if x < 0:
functions 3 return 0.0
The main program 4 elif 0 <= x < 1:
Lambda functions 5 return x
Branching 6 elif 1 <= x < 2:
IF-ELSE blocks 7 return 2 - x
Inline IF-tests 8 elif x >= 2:
9 return 0.0

... or
Functions and
branching
IF-ELSE blocks (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
0, x <0
Function argument v global

variable
x, 0x<1
Beyond math functions N(x) =
Multiple returns
2 x,
1x2

Summation
0, x 2

No returns
Keyword arguments
Doc strings
Functions as arguments to 1 def N ( x ) :
functions 2 if 0 <= x < 1:
The main program
3 return x
Lambda functions
4 elif 1 <= x < 2:
Branching 5 return 2 - x
IF-ELSE blocks 6 else :
Inline IF-tests 7 return 0
Functions and
branching

UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions
Multiple returns
Summation Inline IF-tests
No returns
Keyword arguments
Branching
Doc strings
Functions as arguments to
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
Functions and
branching
Inline IF-test
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Variables are often assigned a value based on a boolean expression
Program flow
Local and global variables
Multiple arguments
Function argument v global This can be coded using a common IF-ELSE test
variable
Beyond math functions
Multiple returns
Definition
Summation
No returns 1 if c o n d i t i o n:
Keyword arguments 2 a = value1
Doc strings 3 else :
Functions as arguments to 4 a = value2
functions
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests
The equivalent one-line syntax, inline IF-test, for the snippet above
1 a = ( value1 if c o n d i t i o n else value2 )
Functions and
branching
Inline IF-test (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
(
sin (x), 0x
Local and global variables
f (x) =
Multiple arguments
Function argument v global
0, otherwise
variable
Beyond math functions
Multiple returns
Summation
Example
No returns
Keyword arguments 1 def f ( x ) :
Doc strings 2 return ( sin ( x ) if 0 <= x <= 2* pi else 0)
Functions as arguments to
functions
The main program
Lambda functions ... or
Branching
IF-ELSE blocks
Example
Inline IF-tests

1 f = lambda x : sin ( x ) if 0 <= x <= 2* pi else 0


Functions and
branching
Inline IF-test (cont.)
UFC/DC
FdP - 2017.1

Functions
Mathematical functions as
Python functions
Program flow
Local and global variables
Multiple arguments
Function argument v global
variable
Beyond math functions Remark
Multiple returns
Summation The IF-ELSE test cannot be used inside an lambda
No returns function, as it has more than one single expression
Keyword arguments
Doc strings Lambda functions cannot have statements
Functions as arguments to
functions
A single expression only
The main program
Lambda functions

Branching
IF-ELSE blocks
Inline IF-tests

You might also like