Control Flow: Functions
Sue Inn Chng
The University of 1
Image credit: https://all-free-download.com/free- vector/download/kids-looking-out-window-clip-art_22948.html
Copyright By PowCoder代写 加微信 powcoder
Introduction
– Recall: Variables allows you to refer to a value by its name.
– Functions allows you to refer to a set of instructions by its (function) name
and it will only execute when its called!
– There are four parts to each function:
1. The function name (what it’s called)
2. Thefunctionarguments(theinformation/variableswepassit) 3. Thereturntype(whatkindofthingisreturnedbythefunction) 4. Thefunctionbody(theactualcodethatdoesthework)
– Function call must be made with the correct function signature. The function name and list of argument types make up the function signature.
The University of 2
Example – main function in C
– There are four parts to each function:
1. The function name (what it’s called)
2. Thefunctionarguments(theinformation/variableswepassit) 3. Thereturntype(whatkindofthingisreturnedbythefunction) 4. Thefunctionbody(theactualcodethatdoesthework)
• Can you spot these parts in the main() function for each language? The University of 3
The University of 4
Example – main function in Python
– There are four parts to each function:
1. The function name (what it’s called)
2. Thefunctionarguments(theinformation/variableswepassit) 3. Thereturntype(whatkindofthingisreturnedbythefunction) 4. Thefunctionbody(theactualcodethatdoesthework)
• Did you manage to spot Item 3 and Item 4?
• Run demo.py on Ed Lesson. What do you see?
The University of 5
Python: Defining Functions
def
statement 2
return
– Keyword def is used to define (create) a function
–
• Should be lowercase with words separated by underscores
• Reflect what the function does e.g. print_array(), get_name()
–
• Function call must match function signature – function name and parameter variables.
– Keyword return
The University of 7
End the execution of the function call and pass the
Cannot be used outside a function
Parameters and Arguments
– Thesetermswillcomeupoften,butareofteninterchangeable – argumentsomethingthatispassedtoafunction
– parametersomethingthatisusedbyafunction
The University of 8
• Identify function name and data types of its parameters.
• How many variables are present in this program?
Functions: Example
– Example of simple function and global codes: Function Definition
Function Header
– Variables in this program:
– Global variables (within file but outside function definition): num1, num2, check
– Local variables (inside function body): x, y, result
– Parameter variables (inside function header): x, y
– Let’s trace the flow of execution. The University of 9
Function Body
Function Call
Flow of Execution: Lines 1 to 8
• Are Lines 2-5 executed now?
The University of 10
Flow of Execution: Line 9
The University of 11
Flow of Execution: Line 9
The University of 12
Flow of Execution: Line 9
The University of 13
Keyword return
– If a return statement is omitted, Python by default returns the value None.
– If your function is intended to return a value then you must include an expression or variable in the return statement.
– return statement is NOT the same as print statement!
– Can only return ONE data type. Multiple values can be pass out of the function by creating a tuple.
Run the program in Ed Lesson. Observe the output.
The University of 14
Type Hinting
– In other languages like Java, programmers must define the data type of the parameters and return value in the function header.
– The Python runtime does not enforce function and variable type annotations.
– Codesinexample(seeimageon right) will NOT have errors despite the type annotation for return value being different from the actual type.
– https://docs.python.org/3/library/ty ping.html
The University of 15
Why use Functions?
– Separate tasks within a program.
– Big problems can be broken into smaller pieces.
– Afunctioncanbewrittentosolveeachpieceofproblemandlaterassembled into a working whole.
– Reuse codes.
– Don’t re-invent the wheel each time.
– Codeonceandre-usebycallingfunctionwithnewdata.
– Reducechanceoferrors.
– Process of copy, paste and modify codes introduces human error.
– Definegeneralimplementationonce.
– Ifachangeismadetotheimplementation,youonlyhavetofixitonce!
The University of 16
– Syntaxtodefinefunctions:
def
statement 2
return
– Differencebetweenthefollowingterms: – Parametersandarguments
– Globalandlocalvariables
– DocumentingPythonprogramsusingdocstringandtypehints.
The University of 17
Reading for this week
– Chapter3.Downey,A.B.(2015).ThinkPython:HowtoThink Like a Computer Scientist (2e ed.). O’ , Incorporated.
The University of 18
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com