Unit 2
Unit 2
Unit 2:
Functions
Function
named sequence of statements that performs a
computation
it is defined by specifying the name and the
sequence of statements
Example:
>>> type(32)
<class ‘int’>
Format of calling a Function
name_of_function(argument)
A function “takes” an argument and “returns”
a result called the return value
An argument is an expression that appears
between the parentheses of a function call
Function Example
int() function
- converts values
into integers
- throws an error if it
cannot convert value
to integer
Function Example
int() function always
rounds down
Why Functions?
Creating a new function gives you an opportunity
to name a group of statements, which makes
your program easier to read and debug
collection of related
functions classes
functions
specific task variables objects
the module object contains
the functions and variables variables
Example:
>>> import math
Module
After creating the module object, you can
access any of its functions or variables by
using this general format:
module_name.function_name()
module_name.variable
this is called the dot notation
Math Module
built-in python module that contains usual
mathematical formulas and values
def function_name(parameter):
body_of_code the expected
keyword that arguments that
indicates that it will take. Can
this is a composition of codes that the be empty
function function will do when called
definition
Example
Function name?
Parameter?
Body?
Example
You can return
values in a
function.
name_of_function(argument)
Recall general format of a function
(with definition): name of the function that
will be used to call it by
def function_name(parameter):
body_of_function
keyword that the expected
indicates that this arguments that it
is a function composition of codes that the will take. Can be
definition function will do when called empty
Parameter and Arguments
Parameter
a variable in a function definition
a placeholder
does not have a concrete value
Argument
value passed when a function is called
Parameter and Arguments
def print_cat_name(cat):
sentence = “My cat’s name is”
print(sentence, cat)
What is the parameter?
food = ‘Mochi’
best_boy = ‘Shrek’ What are the arguments?
print_cat_name(food)
print_cat_name(best_boy)
Parameter and Arguments
Output:
Function parameters and variables
are local.
- meaning they only exist and are accessible
inside of the function definition
The variable sentence cannot be used
outside of the function print_cat_name()
another variable named also as sentence has been
defined outside the print_cat_name() function
Stack Diagrams
used to keep track of which variables can be used
where
show the value of each variable, but they also show
the function each variable belongs to
each function is represented by a frame
a box with the name of a function beside it and the
parameter and variables of the function inside it.
frames are arranged in a stack that indicates which
function called which,and so on.
Stack Diagrams
Stack Diagram
Code
Stack Diagrams
if an error occurs during a function call, Python
prints the names of all the functions involved
this is called a traceback
Fruitful and Void Functions
Fruitful Functions are functions that return results
example is the math function
return value will cease to exist if it is not assigned
to a variable
Void Function are functions that perform an action but
don’t return a value
example is the print_lyrics() functions
if assigned to a variable, that variable will hold the
special value None
Fruitful or Void function?
Fruitful or Void function?
Void
Fruitful
it returns a value
Summary
We’ve learned what a function is, how to
make our own, and how to use them
intuitively.
Exercises
Please do these online exercises during your
own time. No submission needed.
https://holypython.com/beginner-python-exercises