Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Functions in Python

The document provides an overview of functions in Python, explaining their definition, components, and the return statement. It discusses arguments and parameters, including positional, keyword, and default arguments, as well as the scope of variables, differentiating between local and global scope. Additionally, it outlines the flow of execution in Python and describes types of user-defined functions with an example problem.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Functions in Python

The document provides an overview of functions in Python, explaining their definition, components, and the return statement. It discusses arguments and parameters, including positional, keyword, and default arguments, as well as the scope of variables, differentiating between local and global scope. Additionally, it outlines the flow of execution in Python and describes types of user-defined functions with an example problem.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Functions in Python

BY: Preeti Dalal,


Lecturer Computer Science
What do you understand by the term Function?
A Python function is a block of organized, reusable code that is
used to perform a single, related action. In Python, we have:
1. Built-in functions
2. Functions defined in modules
3. User Defined Functions (UDF)

NOTE: Functions are also known as METHODS in


Python.
What are the various components of a function in Python?
Explain return statement in Python?
1. It is the last statement of the function body, where the function gets
terminated.
2. No statement in the function will be executed after the return statement.
3. return statement is not mandatory and should be used only if we want to use
the value processed inside the function in the main program.
4. When a function doesn’t have any return statement it return a legal empty
value None.
5. Python have a special feature that it can return multiple values in a single
return statement.
6. The multiple return values will be stored as TUPLE if we have used one
variable in function call statement to store the returned value.
7. To Unpack the received multiple values, we have to use the equal number of
variables on the left side of function call according to returned number of
values.
Explain return statement in Python? Contd…
.
1. Once, the control exits from a function, it cannot get back
into the function block for any further execution in the same
function call.
2. A function may have more than one termination points. But
only one return statements will pass the control back to the
caller program. For example:
What are Arguments and Parameters?
1. The values being passed through a function call statement
are called arguments or actual arguments or actual
parameters.
2. The values received in the function definition/ header are
called parameters or formal parameters or formal
arguments.
3. There are 3 types of formal arguments:
★ Positional (Required) arguments.
★ Keyword (Named) arguments.
★ Default arguments.
Positional (Required) Arguments
When the function call statement must match the number and order
of arguments as defined in the function definition, this is called the
positional arguments matching.
Keyword (Named) Arguments
Keyword arguments are the named arguments with assigned
values being passed in the function call statement. This allows
to call the function with arguments in any order using name of
the arguments.
Default Arguments
A parameter having predefined value in the function header is
known as a Default argument. It can be skipped in the function
call statement. If we assign a value for a default argument in
function call then the default value will be overwritten.
Rules for combining all 3 types of Arguments:

1. An argument list must contain positional


arguments followed by keyword
arguments.
2. Keyword arguments should be taken from
the required arguments.
3. We cannot specify a value for an argument
more than once.
What do you mean by the Scope of a variable?
SCOPE of a variable is the block of code in the main program
where the variable is declared, used and can be modified.
In Python, there are broadly two scopes of a variable:
1. Local Scope.
2. Global Scope.

NOTE: Scope is also


termed as Lifetime of
a variable in general
terms.
Difference between Local and Global Scope of a variable?

Local Variable Global Variable


A variable that is declared inside a A variable that is declared outside the
function function i.e. in the main program

Accessible only within the function in Accessible by all the functions in the
which it is declared. program.

It is created when the function starts It remains in existence for the entire
executing and is destroyed when the time the program is executing.
function execution is complete.

Its value cannot be changed by other It is accessible by multiple functions,


functions. therefore its value can be changed.
Flow of Execution
Flow of execution refers to the order of execution of the
statements in Python. Execution will start from the first line of the
python program. For Example:
1. def increment(x):
The flow of control will be:
2. X=X+1
3. #main program 1-> 4-> 5->6-> 1-> 2-> 7
4. X=3
5. print(X)
6. increment(X)
7. print(X)
Types of User Defined Functions

1. Function with no arguments and no return.


2. Function with arguments but no return value.
3. Function with arguments and return value.
4. Function with no argument but return value.
Pr. 1. Write a function count(Places) in Python, that takes the dictionary, Places as an argument and
displays the names(in uppercase) of the places whose names are longer than 5 characters.

Output:

LONDON
NEW YORK
Pr. 1. Write a function count(Places) in Python, that takes the dictionary, Places as an argument and
displays the names(in uppercase) of the places whose names are longer than 5 characters.

Output:

LONDON
NEW YORK

You might also like