Chapter-3 Working With Functions
Chapter-3 Working With Functions
INTRODUCTION
A large program is broken down into smaller units known as functions. A function is a named
unit of a group of statements. This can be invoked/ called from other parts of the program.
The most important reason to use functions is to make program handling easier as only a small
part of the program is dealt with at a time, therefore avoiding ambiguity. Another reason to use
functions is to reduce program size. Functions make a program more readable and
understandable to a programmer.
A function is a subprogram that acts on data and often returns a value.
UNDERSTANDING FUNCTIONS
def calculate(x):
r=2*x*x
return r
def means a function definition is starting
identifier following def is the name is the name of the function
Variables inside the parentheses are the arguments or parameters.
There is a colon at the end of the block line, meaning it requires a block.
The statements indented below the function are called the body of the function.
The return statement returns the computed result.
The non-indented statements are not a part of the function
1. Function Header: The first line of the function definition that begins with keyword def
and ends with a colon ( : ), specifies the name of the function and its parameters.
2. Parameters: Variables that are listed within the parenthesis of a function header.
3. Function Body: The block of statements/ indented statements beneath the function
header that defines the action performed by the function. The function body may or may
not return any value. A function returns a value through return statement.
4. Indentation: the blank space in the beginning of the statement within a block. All
statements within same block have same indentation.
The function body or definition of a function executes only when the function is called or
invoked.
Whenever a function call statement is encountered, an execution frame for the called function is
created and the control is transferred to it. With the function’s execution frame, the statements
in the function body are execute, and with the return statement or the last statement of
function body, the control returns to the statement where from the function was called.
Arguments can be literals, variables or expression but the parameters have to be some names,
i.e. variables to hold incoming variables.
The alternative names for arguments are actual parameter or actual arguments.
Alternative name for parameters are formal parameter or formal argument.
If you are pasing values of immutable types to the called function then the called function
cannot alter the values of passed arguments but if you are passing the values of mutable types
then called function would be able to make changes in them.
PASSING PARAMETERS
Python supports three types of formal arguments:
1. Positional arguments (Required arguments)
2. Default arguments
3. Keyword (or named) arguments
Positional/Required Arguments
With the function call statement must match the number and order of arguments as defined in
the function definition; this is called the positional argument matching.
In positional arguments:
The arguments must be provided for all parameters (Required)
The value of arguments are matched with parameters, position (order) wise (Positional)
Default Arguments
Python allows us to assign default values to a function’s parameter which is useful in case a
matching argument is not passed in the function call statement. The default values are specified
in the function header of function definition.
Non-default argument cannot follow default argument i.e. default argument must be the
rightmost argument in function header. The required parameters should be before default
parameters.
A parameter having a default value in function header becomes optional in function call.
Function call may or may not have value for it.
Keyword (Named) Arguments
To have complete control and flexibility over the values sent as arguments for the corresponding
parameters, Python offers keyword arguments.
Keyword arguments are the named arguments with assigned values being passed in the function
call statement.
Ques. Program to calculate simple interest through function. Do specify default values for rate
and time.
Ques. Program that receives two numbers in a function and returns the results of all arithmetic
operations.
COMPOSITION
Composition in general refers to using an expression as part of a larger expression; or a
statement as a part of larger statement. For e.g.
int(str(52))
int(float(“52.5”)*2)
int(str(52)+str(10))
SCOPE OF VARIABLES
Part(s) of program within which a name is legal and accessible is called scope of the name. there
are two kinds of scopes in Python:
Global Scope
A variable declared in top level segment (__main__) of a program is said to have a global scope
and it is usable inside the whole program and all blocks contained in the program.
Local Scope
A variable declared in a function body is said to have local scope i.e. it can be used only within
this function and blocks (for loop, if statement etc.) contained under it. The formal arguments
also have a local scope.
Try This:
1. Variable in global scope but not in Local scope
2. Variable neither in local scope nor in global scope
3. Some variable name in locals scope as well as in global scope (a local variable created
with same name as that of global variable, it hides the global variable)
What if you want to use the global variable inside local scope?
To tell a function that for a particular name, do not create a local variable but use the global
variable instead, write: global <variable name>
Once a variable is declared global in a function, you cannot undo the statement. That is, after a
global statement, the function will always refer to the global variable and local variable cannot
be created of the same name. One should avoid using global statement in program.
MUTABLE/IMMUTABLE PROPERTIES OF PASSED DATA OBJECTS
Python’s variables are not storage containers, rather they are like memory references;
they refer to the memory addresses where the value is stored.
Depending upon the mutability/immutability of its data type, a variable behaves
differently. That is, if a variable is referring to an immutable type then any change in its
value will also change the memory address it is referring to, but if a variable is referring
to mutable type then any change in the value of mutable type will not change the
memory address of the variable.
That means,
Changes in immutable types are not reflected in the caller function at all.
Changes, if any, in mutable types
Are reflected in caller function if its name is not assigned a different variable or
datatype.
Are not reflected in the caller function if it is assigned a different variable or
datatype.