Chapter 7
Chapter 7
Introduction
• The code block always comes after a colon (:) and is indented.
Example:
def Do_Something( ):
value =1 #Assignment Statement
return value #Return Statement
Block:
• A block is one or more lines of code, grouped together so that
they are treated as one big sequence statements while
execution .
• statements in a block are written with indentation.
Nested Block:
• A block within a block is called nested block.
• When the first block statement is indented by a single tab
space, the second block of statement is indented by double
tab spaces.
if(condition):
statement
if(condition):
statement
Advantages of User-defined Functions
• Functions help us to divide a program into
modules. This makes the code easier to manage.
• It implements code reuse. Every time you need to
execute a sequence of statements, all you need
to do is to call the function.
• Functions, allows us to change functionality
easily, and different programmers can work on
different functions.
Calling a Function
Alternate :
If the return has no argument, “None” will be displayed as the last statement
of the output.
Passing Parameters in Functions
Parameters or arguments can be passed to functions
Function Arguments
• Arguments are used to call a function.
• There are primarily 4 types of functions that one can
use: Required arguments, Keyword arguments,
Default arguments and Variable-length arguments.
Required Arguments
• 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.
Instead of printstring() in the above code if we use printstring (“Welcome”)
Keyword Arguments
• Keyword arguments will invoke the function
after the parameters are recognized by their
parameter names.
Default Arguments
• The default argument is an argument that
takes a default value if no value is provided in
the function call.
Variable-Length Arguments
• In some instances you might need to pass
more arguments than have already been
specified
• If the are not specified in the function’s
definition and an asterisk (*) is used to define
such arguments.
• In Variable Length arguments we can pass the
arguments using two methods.
1. Non keyword variable arguments
2. Keyword variable arguments
• Non-keyword variable arguments are called
tuples.
Anonymous Functions
• An anonymous function in Python is a function without a
name. It can be immediately invoked or stored in a variable.
Anonymous functions in Python are also known as lambda
functions.
• Syntax for anonymous functions
The return Statement
Local Scope
• A variable declared inside the function's body is known as local variable.
• Rules of local variable
• A variable with local scope can be accessed only within the function that it
is created in.
• When a variable is created inside the function the variable becomes local
to it.
• A local variable only exists while the function is executing.
• The formal parameters are also local to function.
Global Scope
# Function to multiply
# 2 to a number
def multiply(x):
return x * 2
def fact(n):
if n == 0:
return 1
else:
return n * fact (n-1)
print (fact (0))
print (fact (5))
Output:
1
120