Week05 Review Functions
Week05 Review Functions
Functions
Abstraction in Algorithms:
Functions
statement1
statement2 Function suite:
Keyword Suite of the
indicating • contains code function follows
function is return to perform the colon.
defined. some action
valueToReturn
• indented
tab
Return statement: indicates the
value returned when the function
finishes.
Main Program
statement
25
fahrenheit = cel2fahr(25)
statement call Function
def cel2fahr(celsius):
statement
re t u val = celsius * 1.8 +
rn
77
32
return val
myStr = "Hello"
result = 0
def funcA(content):
num = 0
✔ A.0
for char in content: B.1
num += 1 C.2
D.3
return num E.4
F.5
result = funcA(myStr)
funcA(myStr)
print(result)
5
“The set of program statements over which a variable exists, i.e. can be referred to.”
It is about understanding, for any variable, what its associated value is.
statement1
statement2
…
It looks like a dictionary, and for the most part it is (at least for modules and classes).
Names Objects
var1 23
a 1,345
myString “blah”
Names Objects
def myFunc(param1, param2): var1 23
var1 = 23 vs.
a = 1.345 a 1,345
myString = ‘blah’
myString “blah”
For Python, there are potentially multiple namespaces that could be used to determine
the object associated with a variable name.
Which one to use?
The search order of namespaces for a name:
• Built-in : Finally, check the built-in, defined as part of the special built-ins scope.
• Else, ERROR.
search order:
Global
Local
Enclosing
Enclosing
Local
import
Global
Build-in
Build-in
ERROR
If a reference is assigned in a function, then that reference is only available within that function.
If a reference with the same name is provided outside the function, the reference is reassigned
def enclosing():
myVariable = 'defined by enclosing'
def enclosed():
print('scope: ' + myVariable)
enclosed()
enclosing()
What if you really want to use or modify the global variables inside a function?
Ans: Using the global keyword in a function allows you to access global objects.
myVar = 100
Global Namespace Objects
Each function maintains a namespace for names defined locally within the function.
For each argument in the function invocation, the argument’s associated object is passed
to the corresponding parameter in the function.
The argument and the parameter share an association with the same object.
This assignment does not affect the object associated with the argument.
When passing a mutable data structure, if the shared object is modified, both the
parameter and the argument will reflect that change.
Program A Program B
A. Yes
✔ B. No
myList = [1,2,3] 1 2 3 4
newList = myFun(myList)
print(myList,newList) myFun Namespace
param
Program A
myList = [1,2,3] 1 2 3
newList = myFun(myList)
print(myList,newList) myFun Namespace
param 1 2 3 4
Program B
myVar = 127
A.myVar: 7
def myFun (myVar): myVar: 7
myVar = 7
print(‘myVar: ’, myVar) B.myVar: 127
myVar: 127
myFun(myVar)
print(‘myVar: ’, myVar)
✔ C.myVar: 7
myVar: 127
D.myVar: 127
myVar: 7
myVar = 127
myFun()
✔ A.a: 128
B.a: 127
C.ERROR