Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
29 views

Python Lecture - 12

Functions allow grouping code to perform repeated tasks or achieve a specific goal. Functions improve code organization, readability, and reusability. Variables declared within a function have local scope, while global variables defined outside can be accessed by any code. Arguments passed to functions can be immutable values like strings or mutable objects like lists, which are passed by reference and could be modified within the function. Functions return control and potentially values back to the main program flow.

Uploaded by

let.me.g0.h0m3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Python Lecture - 12

Functions allow grouping code to perform repeated tasks or achieve a specific goal. Functions improve code organization, readability, and reusability. Variables declared within a function have local scope, while global variables defined outside can be accessed by any code. Arguments passed to functions can be immutable values like strings or mutable objects like lists, which are passed by reference and could be modified within the function. Functions return control and potentially values back to the main program flow.

Uploaded by

let.me.g0.h0m3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Functions, Scope &

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.

• Functions are code blocks outside the normal


execution of a program and need to be “called” by the
main program.

• Functions are typically used to perform repeated tasks.

• Functions improve readability and reusability of code.


Role of Functions

Return
Arguments

Mutables
Global
variables Function
Global
variables
Local
Files variables
Files

Image Credit: Learning Python by Mark Lutz


Structure of a function
• In the main program a function is called by its name:
• myFunction(a,b,c)
• A function is defined using the keyword def:

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

Image adapted from Learning Pyton by Mark Lutz


Argument passing with mutables

Name Object

person
‘Bob’
caller
ages ‘Smith’

a 25,15,35
function
b

Image adapted from Learning Pyton by Mark Lutz


Maintaining integrity of mutables
• Integrity of lists and other mutables can be
maintained by passing an explicit copy.
• For example:

Sends a copy of the list ages

Sends a reference to the list ages


Summary: Using functions in your project

• Functions allow separation of code into logical units to


improve code readability.
• Functions allow reusing code i.e., ‘cut and paste’ to
move code across your scripts. Eg., a function to read
FASTA files into a dictionary can be reused across any
script that needs to read FASTA files.
• Limits on scope of variables allows reusing variable
names while still maintaining data integrity.
• Beware of passing mutable objects as arguments.

You might also like