Class XII - L3 - Working With Functions
Class XII - L3 - Working With Functions
1
3.1 UNDERSTANDING A FUNCTION:
• Consider the example
def calcSomething (x):
r = 2 *x ** 2
return r
where,
• def means a function definition is starting.
• Identifier following ‘def’ is the name of the function, i.e., the function name is
calcSomething.
• The variable/identifiers inside the parentheses are the arguments or
parameters, i.e., here x is the argument to function calcSomething.
• There is a colon at the end of def line, meaning it requires a block.
• The statement indented below the def function, define the functionality
of the function. This block is called body-of-the-function.
• The return statement returns the computed result.
2
3.1.2 Python Function Type:
Python comes preloaded with many function-definitions that we can use
as per the needs. There are three following categories:
1. Built-in functions:
These are pre-defined functions and are always available for use.
Like len( ), type( ), int( ), input( ), etc.
2. Functions defined in modules:
These functions are pre-defined in particular modules and can only
be used when the corresponding module is imported.
Example : if you want to use sign() we need to import math
3. User defined functions:
These are defined by the programmer. As programmers can create
their own functions.
3
3.2.1
Structure of A Python Program:
• In python, all function definitions are given at the top followed by
statements which are not part of any functions. These are called
top-level statements.
• The python interpreter starts the execution of a program/ script
from the top-level statements.
• The top level statements are part of the main program, python
gives a special name as _main_.
4
3.3.1 Structure of A Python Program:
• The top level statements are part of the main program, python gives
a special name as _main_.
def function1( ) :
…..
def function2( ) :
…..
def function3( ) :
…..
Statement 1
Statement 2
…….
• Example:
def greet( ) :
print(“ Hi there!”)
print(“Inside”,_name_)
5
3.4 FLOW OF EXECUTION IN A FUNCTION:
• The flow of execution refers to the order in which statements are executed
during a program run.
• An execution frame contains:
i. Some internal information.
ii. Name of the function.
iii. Values passed to function.
iv. Variables created within function
v. Information about the next instruction to be executed
• Whenever a function call statement is encountered, an execution frame for
the called function is created and the control is transferred to it.
• Within the functions execution frame, the statements in the function-body
are executed, and with the return statement or the last statement of
function body, the control returns to the statement wherefrom the function
was called.
• Program execution begins with the first statement of _main_ segment
• Python starts reading from line 1 downwards. Statements are executed one
at a time, in order from top to bottom. Python follows the guidelines while
executing:
i. Execution always begins at the first statement of the program.
ii. Comment lines are ignored and not executed.
iii. If python notices that it is a function definition, then python just executes
the function header line to determine that it is proper function header
and skips all lines in the function body.
iv. The statements inside a function-body are not executed until the function
is called.
v. A function can define another function inside it.
vi. When a code-line contains a function call, python first jumps to the
function header line and then to the first line of the function body and starts
executing it.
6
vii. A function ends with a return statement or the last statement of function
body, whichever occurs earlier.
viii. If the called function returns a value then the control jump back to the
function call statement and completes it.
ix. If the called function does not return any value, then the controls jumps
back to the line following the function call statement.
7
3.5 PASSING PARAMETERS:
• A function call must provide all the values as required by function definition.
If a function header has three parameters named in its header then the
function call should also pass three values.
• Python supports three types of formal arguments/parameters:
1. Positional arguments.
2. Default arguments.
3. Keywords.
8
• The default value is specified in a manner syntactically similar to a
variable initialization. In the above example, function declaration
provides a default value of 0.10 to the parameter rate.
• In a function header, any parameter cannot have a default value
unless all parameter appearing on its right have their default values.
• The default values for parameters are considered only if no value is
provided in the function call statement.
• Advantages of default parameters:
1. They can be used to add new parameters to the existing functions.
2. They can be used to combine similar functions into one.
9
• The second argument time = 5 is representing the keyword argument.
• Rules for combining all three types of argument:
1. An argument list must first contain positional argument followed
by key word argument.
2. Keyword should be taken from the required arguments
preferably.
3. You cannot specify a value for an argument more than once.
10
2. Functions not returning any values:
• The functions that perform some actions or do some task but not return
any computed values or final value to the caller are called void
functions.
• The void function do not return a value but they return a legal empty
value of python. Every void function returns value None to its caller
3.7 COMPOSITION:
1. An arithmetic expression.
2. Logical expression
3. A function call.
• The scope rules of a language are the rules that decide, in which parts of
the program, a particular piece of code or data item would be known and
can be accessed within.
• Parts of program within which a name is legal and accessible is called scope of
the name.
11
• There are broadly two kinds of scopes in python:
1. Global scope:
• A name declared in top level segment of a program is said to
have a global scope and is usable inside the whole program
and all blocks within the program.
• A global variable is a variable defined in the main program
(_main _ section). Such variables are said to have global scope.
2. Local scope:
• A name declared in a function-body is said to have local
scope. it can be used only within this function and the other
blocks contained under it.
• A local scope can be multi-level, there can be an enclosing
local scope having a nested local scope of an inside block.
• A local variable is a variable defined within a function, such
variables are said to have local scope.
12
iv. Python checks its Built-in environment (LEGB) that contains all built-in
variables and functions of python, if there is a variable with the
same name, if yes, python uses its value.
v. Otherwise python would report error.
13