Week7 Funtions
Week7 Funtions
Functions
Functions
• A function (or sub-routine, procedure or sub-
program)
• Is a grouping of actions under a given name that
performs a specific task
• len(), print(), abs(), type(): are functions
that we have seen before
• Pyhon adopts a syntax that remebles mathematical
functions, e.g.
• f(x, y) = 3x + 4y + 1
Defining Functions
G = 6.67408E-11
force = F_gravity(1000, 20, 0.5)
print(force, “Newton”)
Passing parameters to functions
•We can swap the order of default parameters when we are calling
the function (CASE 5 and CASE 6).
We demonstrate the
enclosing scope using
nested functions in
this example
Scope of varibles: Global Scope
• In both examples: variable b is defined in the global
scope
Scope of varibles: Built-In Scope
• The variable e is defined in the math package. It is
built-in
Scope of varibles: Built-In Scope
• If another variable e is defined in the global or local
scope, it is used instead of the built-in version
Lecture summary
• Functions
• How to define functions
• Passing default parameters to functions
• Scope of variables