Python Functions
Python Functions
Functions
8-Feb-22
Python Functions
• There are two kinds of functions in
Python.
• Built-in functions that are provided as part
of Python - raw_input(), type(), float(),
int() ...
• Functions that we define ourselves and
then use
• We treat the of the built-in function
names as "new" reserved words (i.e.
we avoid them as variable names)
Function Definition
• In Python a function is some reusable
code that takes arguments(s) as input
does some computation and then
returns a result or results
• We define a function using the def
reserved word
• We call/invoke the function by using
the function name, parenthesis and
arguments in an expression
Python BuiltinArgument
Functions
Assignment
'w'
Result
def print_lyrics():
print "I'm a lumberjack, and I'm okay.”
print 'I sleep all night and I work all day.'
print 'Yo'
print_lyrics() Hello
x=x+2 Yo
print x I'm a lumberjack, and I'm
okay.I sleep all night and I
work all day.
7
Arguments
• An argument is a value we pass into the function as
its input when we call the function
• We put the arguments in parenthesis after the name
of the function
def greet():
return "Hello” Hello Glenn
Hello Sally
print greet(), "Glenn”
print greet(), "Sally"
Functions without returns
All functions in Python have a return value,
even if no return line inside the code
Functions without a return return the special
value None
None is a special constant in the language
None is used like NULL, void, or nil in other
languages
None is also logically equivalent to False
The interpreter’s REPL doesn’t print None
Function overloading? No.