Professional Documents
Culture Documents
Chapter - 3 Notes
Chapter - 3 Notes
FUNCTIONS:
Types of functions:
i) Built-in functions
ii) Functions defined in module
iii) User defined functions
These are pre-defined functions and are always available for use.
In order to use these functions , there is no need to import any module.
We can directly use it , in our program.
Ex: add(),area(),fact(),display(),compute(),LCM()
Def functionName([parameters]):
<statement>
[<statement>]
Yes , a function can return more than one value in the form of tuple.
Flow of execution:
The flow of execution refers to the order in which statements are executed during a program run.
Ex:
Passing parameters:
Default arguments:
Default is an argument that assumes a default value if a value is not provided in the function
call for that argument.
If you are not passing any value, then only default values will be considered.
Non default arguments cannot follow default arguments.
In a function header,any parameter cannot have a default value unless all parameter appearing on
its right have their default values
Ex:
The default arguments gives flexibility to specify default value for a parameter so that it can be
skipped in the function call,if needed.
However, still we can not change the order of arguments in function call.
To get control and flexibility over the values sent as arguments , python offers keyword
arguments.
This allows to call function with arguyments in any order using name of the arguments.
Ex:
Rules for combining all three types of arguments:
An argument list must first contain positional arguments followed by any keyword arguments.
Keyword arguments should be taken from the required arguments preferable.
You cannot specify a value for an argument more than once.
Scope of a variable:
Local scope:
The variables declared inside the function definition are called local variables.
Ex: def func():
Print(“inside func1..”,L)
Func1( )
Global scope:
The variables declared outside the function definition are called global variables.
Ex:
If in any program, there are two variables with same name one is local and one is global,to use he
global variable in a function we have to use the global keyword with variable name.
When a function changes the value of a global variable,it will be reflected back outsine the
function also.
LEGB rule:
1. Local scope
Contains names defined inside the current definition.
2. Enclosing scope
Contains names defined inside any and all enclosed function.
3. Global scope
Contains names defined at the top level of the script or module.
4. Built in scope
Contains names built-in to the python language.
MUTABLE AND IMMUTABLE PROPERTIES OF DATA OBJECTS:
Depending upon the mutability or immutability of the data type , a variable behaves differently.
Mutable type , immutable type
Immutable type:
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.
Any change in the value of a immutable type will not get reflected in the original value outside the
function.
Mutable type:
If a variable is referring to mutable type,and then any change in the value of mutable type will not
change the memory address of the variable,it will change the value in place.
Any change in the value of a mutable type will get reflect in the original value outside the function
also.
Note:
If a parameter is assigned a new name or different data type value, then changes will not reflected
back in original value.