You are on page 1of 16

Functions

Functions
• A function is like a subprogram, a small program
inside of a program
• The part of the program that creates a function is
called a function definition.
• When the function is used in a program, we say
the definition is called or invoked.
• The variables used inside of one function are
local to that function, even if they happen to
have the same name as variables that appear
inside of another function
• Four kinds of functions can be created in
Python:
– global functions
– local functions
– lambda functions
– methods.
• Global objects (including functions) are accessible
to any code in the same module (i.e., the same .py
file)
• Local functions (also called nested functions) are
functions that are defined inside other functions.
These functions are visible only to the function
where they are defined
• Lambda functions are expressions, so they can be
created at their point of use; however, they are much
more limited than normal functions.
• Methods are functions that are associated with a
particular data type and can be used only in
conjunction with the data type
Function definition
• A function definition looks like this:
def <name>(<formal-parameters>):
<body>
• The name of the function must be an identifier
• Formal-parameters is a possibly empty list of
variable names
• Formal parameters, like all variables used in the
function, are only accessible in the body of the
function.
Function calling
• A function is called by using its name followed by a list
of actual parameters or arguments.
<name>(<actual-parameters>)
• When Python comes to a function call, it initiates a
four-step process.
– The calling program suspends execution at the point of the
call.
– The formal parameters of the function get assigned the
values supplied by the actual parameters in the call.
– The body of the function is executed.
– Control returns to the point just after where the function
was called.
• The formal and actual parameters are matched up
based on position, e.g. the first actual parameter is
assigned to the first formal parameter, the second
actual parameter is assigned to the second formal
parameter, etc.
• When Python encounters return, it exits the
function and returns control to the point where the
function was called.
• In addition, the value(s) provided in the return
statement are sent back to the caller all Python
functions return a value, whether they contain a
return statement or not. Functions without a
return hand back a special object, denoted None.
• One can call a function by using the following
types of formal arguments
– Required arguments
– Keyword arguments
– Default arguments
– Variable-length arguments
Required arguments
• Required arguments are the arguments
passed to a function in correct positional
order. Here, the number of arguments in the
function call should match exactly with the
function definition.
• If we give too few or too many arguments, a
TypeError exception will be raised.
Keyword arguments
• Keyword arguments are related to the function
calls. When you use keyword arguments in a
function call, the caller identifies the arguments
by the parameter name.
• This allows you to skip arguments or place them
out of order because the Python interpreter is
able to use the keywords provided to match the
values with parameters.
Default arguments
• A default argument is an argument that assumes a default
value if a value is not provided in the function call for that
argument.
– def add(a,b=5,c=1): return(a+b+c)
– add(10,20,30)#60
– add(10,20)#10+20+1=31
– add(1)#1+5+1=7
• The parameter syntax does not permit us to follow
parameters with default values with parameters that don’t
have defaults, so
– def bad(a, b=1, c): won’t work
• Default value should be assign from right to left
Variable-length arguments
• * operator is used for parameter unpacking
• def product(*args):
result = 1
for arg in args:
result *= arg
return result
• product(1, 2, 3, 4) # args == (1, 2, 3, 4); returns: 24
product(5, 3, 8) # args == (5, 3, 8); returns: 120
product(11) # args == (11,); returns: 11
Lambda functions
• Lambda functions are functions created using the
following syntax:

lambda parameters: expression

• The parameters are optional, and if supplied they


are normally just comma separated variable names,
that is, positional arguments, although the
complete argument syntax supported by def
statements can be used.
• The expression cannot contain branches or loops
(although conditional expressions are allowed)
• cannot have a return statement.
• The result of a lambda expression is an
anonymous function.
• When a lambda function is called it returns the
result of computing the expression as its result.
• If the expression is a tuple it should be enclosed
in parentheses.
• area = lambda b, h: 0.5 * b * h

• def area(b, h):


return 0.5 * b * h
Assertion
• The assert Statement. When it encounters an
assert statement, Python evaluates the
accompanying expression, which is hopefully
true.
• If the expression is false, Python raises an
AssertionError exception
• If the assertion fails, Python uses
ArgumentExpression as the argument for the
AssertionError.
• The syntax for assert is −
– assert boolean_expression [, Arguments]

You might also like