0% found this document useful (0 votes)
25 views30 pages

Chapter 7

Functions allow programmers to organize code into reusable blocks. There are several types of functions in Python including: 1) Regular functions which can take arguments and return values. Arguments allow inputting data and return allows outputting results. 2) Lambda functions which are small anonymous functions useful for one-time use. 3) Recursive functions which call themselves, useful for things like calculating factorials. Defining functions, calling them, and managing argument passing helps make code more modular and reusable.

Uploaded by

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

Chapter 7

Functions allow programmers to organize code into reusable blocks. There are several types of functions in Python including: 1) Regular functions which can take arguments and return values. Arguments allow inputting data and return allows outputting results. 2) Lambda functions which are small anonymous functions useful for one-time use. 3) Recursive functions which call themselves, useful for things like calculating factorials. Defining functions, calling them, and managing argument passing helps make code more modular and reusable.

Uploaded by

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

PYTHON FUNCTIONS

Introduction

• Functions are named blocks of code that are designed to do


specific job.
• To perform a particular task that you have defined in a
function, you call the name of the function responsible for it.
• To perform the same task multiple, you just call the function
dedicated to handling that task .

Main advantages of functions are


• It avoids repetition and makes high degree of code reusing.
• It provides better modularity for your application.
Types of Functions

A lambda function is a small anonymous


function. A lambda function can take any
number of arguments, but can only have one
expression.
Defining Functions
• Functions must be defined, to create and use certain
functionality
• Function blocks begin with the keyword “def”
followed by function name and parenthesis ().
Syntax for User defined function
• Any input parameters or arguments should be placed within these
parentheses when you define a function.

• The code block always comes after a colon (:) and is indented.

• The statement “return [expression]” exits a function, optionally


passing back an expression to the caller. A “return” with no
arguments is the same as return None.

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

• The return statement causes your function to exit and


returns a value to its caller. The point of functions in
general is to take inputs and return something.
• The return statement is used when a function is ready to
return a value to its caller. So, only one return statement
is executed at run time even though the function
contains multiple return statements.
• Any number of 'return' statements are allowed in a
function definition but only one of them is executed at
run time.
Syntax of return
• This statement can contain expression which gets evaluated
and the value is returned.
• If there is no expression in the statement or the return
statement itself is not present inside a function, then the
function will return the None object.
Scope of Variables
• Scope of variable refers to the part of the program, where it can be
accessible from.
• There are two types of scopes - local scope and global scope

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

A variable, with global scope can be used anywhere in


the program. It can be created by defining a variable
outside the scope of any function/block.

Rules of global Keyword


When we define a variable outside a function, it’s global
by default. You don’t have to use global keyword.
• We use global keyword to read and write a global
variable inside a function.
• Use of global keyword outside a function has no effect
Without using the global keyword we cannot modify the
global variable inside the function but we can only access the
global variable.
Composition in functions
• Function composition is the way of combining
two or more functions in such a way that the
output of one function becomes the input of
the second function.
• For example, let there be two functions “F”
and “G” and their composition can be
represented as F(G(x)) where “x” is the
argument and output of G(x) function will
become the input of F() function.
EXAMPLE:
# Function to add 2
# to a number
def add(x):
return x + 2

# Function to multiply
# 2 to a number
def multiply(x):
return x * 2

# Printing the result of


# composition of add and
# multiply to add 2 to a number
# and then multiply by 2
print("Adding 2 to 5 and multiplying the result with 2: ",
multiply(add(5)))
Recursive functions
• When a function calls itself is known as
recursion
• A recursive function calls itself.
• Infinite iteration is when a process would
iterate indefinitely if not stopped by some
condition
• The condition that is applied in any recursive
function is known as base condition.
Overview of how recursive function works

• Recursive function is called by some external


code.
• If the base condition is met then the program
gives meaningful output and exits.
• Otherwise, function does some required
processing and then calls itself to continue
recursion.
Example

def fact(n):
if n == 0:
return 1
else:
return n * fact (n-1)
print (fact (0))
print (fact (5))

Output:
1
120

You might also like