03 - Functions and branching

Functions and branching

UFC/DC FdP - 2019.1

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

Branching IF-ELSE blocks Inline IF-tests

019 Functions and branching 2 Foundation of programming (CK0030) JUN ?08F,C ? Francesco Corona

Functions and branching

UFC/DC FdP - 2019.1

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

Branching IF-ELSE blocks Inline IF-tests

FdP (cont)

2019 Two fundamental and extremely useful programming concepts , ? Functions, defined by the user JUN ?08FC ? ? Branching, of program flow

Functions and branching

UFC/DC FdP - 2019.1

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

Branching IF-ELSE blocks Inline IF-tests

FdP

19 Intro to variables, objects, modules, and text formatting 0 Programming with WHILE- and FOR-loops, and lists 2 Functions and IF-ELSE tests 8, ? Data reading/writing, error handling and making modules 0 C Arrays and array computing JUN ? F Plotting curves and surfaces

Functions and branching

UFC/DC FdP - 2019.1

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

Branching IF-ELSE blocks Inline IF-tests

2019 Functions JUN ?08F,C ? Functions and branching

Functions and branching

UFC/DC FdP - 2019.1

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

Branching IF-ELSE blocks Inline IF-tests

Functions

The term function has a wider meaning than a mathematical function

Definition

9 Function 01 A function is a collection of statements that can be run wherever and when-

ever needed in the program

2 The function may accept input variables , ? ? To influence what is computed inside 8 ? (A function contains statements) 0 C The function may return new objects N F Functions help avoid duplicating bits of code (puts all of them together) U ? ? A strategy that saves typing and makes it easier to modify code J Functions are also used to split a long program into smaller pieces

Python has pre-defined functions (math.sqrt, range, len, math.exp, ...) We discuss how to define own functions

Functions and branching

Math functions as Python functions

UFC/DC FdP - 2019.1

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

Branching IF-ELSE blocks Inline IF-tests

9 We construct a Python function that evaluates a mathematical function 01 Example 2 Consider a function F (C ) for converting degree Celsius C to Fahrenheit F

,9 ? F(C ) = C + 32 85 0 C The function (F) takes C (C ) as its input argument N F 1 def F(C):

2 return (9.0/5)*C + 32

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

Functions and branching

UFC/DC FdP - 2019.1

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

Branching IF-ELSE blocks Inline IF-tests

2019 Mathematical functions , ? as Python functions JUN ?08FCFunctions

Functions and branching

UFC/DC FdP - 2019.1

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

Branching IF-ELSE blocks Inline IF-tests

Math functions as Python functions (cont.)

019 All Python functions begin with def, followed by the function name 2 Inside parentheses, a comma-separated list of function arguments

The argument acts as a standard variable inside the function

8, ? The statements to be performed inside the function must be indented 0 After the function, it is common (not necessary) to return a value JUN ? FC The function output value is sent out of the function

Functions and branching

Math functions as Python functions (cont.)

UFC/DC FdP - 2019.1

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

Branching IF-ELSE blocks Inline IF-tests

Example

9 The function name is F (F ) 1 9

0 F(C ) = C + 32 5

2 There is only one input argument C (C ) , 1 def F(C): ? 2 return (9.0/5) *C + 32 08 The return value is computed as (9.0/5)*C + 32 (it has no name) C ? It is the evaluation of F (C ) (implicitly F(C))

N F The def line (function name and arguments) is the function header

U ? The indented statements are the function body

1 def F(C):

J 2 return (9.0/5) *C + 32

#

Function header

# Function (mini ) block

The return often (not necessarily) associates with the function name

Functions and branching

Math functions as Python functions (cont.)

UFC/DC FdP - 2019.1

Functions

Mathematical functions as Python functions

Local and global

1

variables

2

Multiple arguments

3

Function argument v 4

global variable

5

Beyond math functions

6

7

Multiple returns

8

Summation 9

No returns

10

Keyword arguments 11

Doc strings

12

Functions as

13

arguments to

functions

14

The main program 15

Lambda functions

16

Branching IF-ELSE blocks Inline IF-tests

19 # #######################################################################

def F(C):

# T conversion function #

0 return (9.0/5) *C + 32

#

F(C) #

########################################################################

2 temp1 = F (15.5)

#

Store return value as variable (temp1 )

, ? a = 10 8 temp2 = F(a)

#

Given input argument 'a' (value 10)

#

Store return value as variable (temp2 )

0 C print F(a+1) JUN ? F sum_temp = F(10) + F(20)

# Given input argument 'a+1' (value 10 + 1) # Print return value to screen (no storing)

#

Two calls to get two output values

#

Combine output values and store

Functions and branching

Math functions as Python functions (cont.)

UFC/DC FdP - 2019.1

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

Branching IF-ELSE blocks Inline IF-tests

To use a function, we must call or invoke it with input arguments

9 The function will process the input arguments 1 As a result, it will return an output value 0 We (may need to) store the result in a variable

2 Example

, ? 1 # #######################################################################

8 2 def F(C):

# T conversion function #

3 return (9.0/5)*C + 32

#

F(C) #

0 4 # ######### ## # ## # ## # ## # ## # ## # ## # ## # ## # ## # ## # ## # ## # ### ## # ## # ## # ## # ## # ## # ##

N FC The value returned from F(C) is an object

Specifically, it is a float object

JU ? The call F(C) can be placed anywhere in a code

? A float must be valid

Functions and branching

Math functions as Python functions (cont.)

UFC/DC FdP - 2019.1

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

Branching IF-ELSE blocks Inline IF-tests

19 Example 0 Consider the usual list Cdegrees of temperatures in degrees Celsius 2 ? Interest in computing a list of corresponding Fahrenheits

? We want to use function F, in a list comprehension

8, ? 1 # #######################################################################

2 def F(C):

# T conversion function #

0 3 return (9.0/5) *C + 32

#

F(C) #

C 4 # ######### ## # ## # ## # ## # ## # ## # ## # ## # ## # ## # ## # ## # ## # ### ## # ## # ## # ## # ## # ## # ##

5

N F 6 Cdegrees = [ -20 , -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35]

7

JU ? 8 Fdegrees = [F(C) for C in Cdegrees ]

Functions and branching

Math functions as Python functions (cont.)

UFC/DC FdP - 2019.1

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

Branching IF-ELSE blocks Inline IF-tests

Example

9 Consider a slight variation of the F(C) function 1 F2(C) 0 We define F2(C) to return a formatted string 2 (Instead of a real number)

8, ? 1 # #######################################################################

2 def F2(C):

#

0 3 F_value = (9.0/5) *C + 32

#

C 4 return '%.1 f degrees Celsius correspond to '\

#

5

'%.1f degrees Fahrenheit ' % (C, F_value)

#

6 ########################################################################

N F How to use this new function? U ? 1 >>> s1 = F2(21) J 2

3 >>> print s1

4

21.0 degrees Celsius correspond to 69.8 Fahrenheits

Functions and branching

Math functions as Python functions (cont.)

UFC/DC

FdP - 2019.1

Functions

Example 9 Mathematical

functions as Python functions

Consider the construction of a temperature-conversion program c2f.py

1 Local and global

variables

0 Multiple arguments

1

Function argument v 2

2 global variable

3

Beyond math

4

functions

, 5

Multiple returns

? 6

Summation

8 7

No returns

8

0 Keyword arguments

9

C Doc strings

10

Functions as

11

arguments to

functions

12

########################################################################

def F(C):

#

F = 9./5* C + 32

#

return F

#

########################################################################

dC = 10 C = -30

while C >> c1 = 37.5 C 2

3 >>> s2 = F2(c1)

N F 4

5 >>> F_value 6 ...

JU ? 7 NameError : name 'F_value ' is not defined

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download