0% found this document useful (0 votes)
9 views

Python Functions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Python Functions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Python

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

big = max('Hello world')

Assignment
'w'
Result

>>> big = max('Hello world')


>>> print big w
>>> tiny = min('Hello world')
>>> print tiny>>>
Python Functions-Defined
x=5
print 'Hello'

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

big = max('Hello world')


Argument
Parameters >>> def greet(lang):
... if lang == 'es':
... print 'Hola’
 A parameter is a ... elif lang == 'fr':
variable which we ... print 'Bonjour’
use in the function ... else:
definition that is a ... print 'Hello’
“handle” that ...
allows the code in >>> greet('en')
the function to Hello
access the >>> greet('es')
arguments for a Hola
particular function >>> greet('fr')
invocation. Bonjour
>>>
Return Values
• Often a function will take its arguments, do
some computation and return a value to be
used as the value of the function call in the
calling expression. The return keyword is
used for this.

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.

 There is no function overloading in Python


 Unlike C++, a Python function is specified
by its name alone
The number, order, names, or types of arguments
cannot be used to distinguish between two
functions with the same name
 Two different functions can’t have the
same name, even if they have different
arguments
Default Values for Arguments
 You can provide default values for a
function’s arguments
 These arguments are optional when the
function is called

>>> def myfun(b, c=3, d=“hello”):


return b + c
>>> myfun(5,3,”hello”)
>>> myfun(5,3)
>>> myfun(5)

All of the above function calls return 8


Keyword Arguments
 Can call a function with some/all of its arguments
out of order as long as you specify their names
>>> def foo(x,y,z): return(2*x,4*y,8*z)
>>> foo(2,3,4)
(4, 12, 32)
>>> foo(z=4, y=2, x=3)
(6, 8, 32)
>>> foo(-2, z=-4, y=-3)
(-4, -12, -32)
 Can be combined with defaults, too
>>> def foo(x=1,y=2,z=3):
return(2*x,4*y,8*z)
>>> foo()
(2, 8, 24)
>>> foo(z=100)
(2, 8, 800)
Functions are first-class objects
Functions can be used as any other
datatype, eg:
 Arguments to function
 Return values of functions
 Assigned to variables
 Parts of tuples, lists, etc
>>> def square(x): return x*x
>>> def applier(q, x): return q(x)
>>> applier(square, 7)
49
Lambda Notation
 Python’s lambda creates anonymous
functions
>>> applier(lambda z: z * 2, 7)
14
 Note: only one expression in the lambda
body; its value is always returned
 Python supports functional programming
idioms: map, filter, closures,
continuations, etc.
Lambda Notation
Be careful with the syntax
>>> f = lambda x,y : 2 * x + y
>>> f
<function <lambda> at 0x87d30>
>>> f(3, 4)
10
>>> v = lambda x: x*x(100)
>>> v
<function <lambda> at 0x87df0>
>>> v = (lambda x: x*x)(100)
>>> v
10000

You might also like