Python Lecture - 12
Python Lecture - 12
Arguments
HORT 59000
Lecture 12
Instructor: Kranthi Varala
Functions
• Functions are logical groupings of statements to
achieve a task. For example, a function to calculate the
average of a sequence of numbers.
Return
Arguments
Mutables
Global
variables Function
Global
variables
Local
Files variables
Files
def myFunction(x,y,z):
Sum = x+y+z
print(“Sum is :” + Sum)
return “Done.”
• Note: Remember to define a function before calling it.
This is because Python interpreter goes line-by-line
and doesn’t know things that are not yet defined.
Creating a function : def vs. lambda
• def is a key word used to “define” a block of executable
code Code within the def block is not available to the
interpreter until called.
• def creates a function object and assigns the object to
the name of the function.
• def can appear as a separate code block in the same
python file, or inside a loop or condition or even within
another function (enclosed).
• lambda creates a function object as well, but it has no
name. Instead it simply returns the result of the
function.
Control flow: call and return
• When a function is called the flow of the main program
stops and sends the “control” to the function.
• No code in the main program will be executed as long as
the function has the control.
• Function “returns” the control to the main program using the
return statement.
• Every function has an implicit return statement.
• We can use the return statement to send a value or object
back to the main function.
• Since return can send back any object you can send
multiple values by packing them into a tuple.
• If no return statement is given the function returns the None
object.
Example of a function
Example of a function
Example of a reusable function
Example of a reusable function
Scope
• Namespace is the complete list of names, including all
objects, functions, etc. that exist in a given context.
• The scope of an object is the namespace within which
the object is available.
• A variable/object created in the main program is
available all through the program i.e., global
namespace
• A variable/object created within a function is only
available within that function i.e, local namespace
• When a variable name is used the Python interpreter
looks for that variable within the relevant scope first.
LEGB rule for Scope
• Python interpreter follows the LEGB rule for identifying
the object by name.
• Local first: Look for this name first in the local
namespace and use local version if available. If not, go
to higher namespace.
• Enclosing second: If the current function is enclosed
within another function, look for the name in that outer
function. If not, go to higher namespace.
• Global third: Look for the name in the objects defined
at global scale (e.g., main program).
• Built-in last: Finally, look for the variable among
Python’s built-in names.
Scope: Built-ins
Keeping track of Scope
• x and y are
local variables
that only exist
in the scope of
the function
mySum
• BUT, mySum
itself is a
global function
that exists in
the global
scope.
Output:
Keeping track of Scope
• a and b are
local variables
that only exist
in the scope of
the function
mySum
• Changing the
value of local
‘a’ does not
affect global
‘a’
Output:
Using global variables in functions
• Forces a to be
pulled from the
global space.
• Any changes
made to the
global variable
here will affect
the variable
outside the
function as
well.
Output:
Argument passing
• The objects sent to a function are called its arguments.
Arguments are sometimes also called parameters.
• Passing an object as a argument to a function passes
a reference to that object.
• Argument names in the def line of the function become
new, local names.
• Arguments are passed in two ways:
• ‘Immutables’ are passed by value eg., String, Integer, Tuple
• ‘Mutables’ are passed by reference eg., Lists
Argument passing with mutables
• person is a string
(immutable) and
thus passed by
value to the local
variable a.
• ages is a list
(mutable) and thus
passed to b as a
reference.
• a and b are both
Output: local variables, but
b is the same object
as ages.
Argument passing with mutables
Name Object
person
caller ‘Bob’
ages
a 5,15,35
function
b
Name Object
person
‘Bob’
caller
ages ‘Smith’
a 25,15,35
function
b