CH 3 Function
CH 3 Function
com
Function
Function
Definition
OUTPUT
Function Definition
OUTPUT
Unit-I: Programming and Computational Thinking-2 Visit: https://dineshkvs.wordpress.com
Example 2:
Formal Arguments
Actual Arguments
Types of Arguments:-
There are 4 types of Actual Arguments allowed in Python:
1. Positional arguments
2. Default arguments
3. Keyword arguments
4. Variable length arguments
1.Positional arguments:- These are the arguments passed to afunctionin
correct positional order.
Here the Value of A and B passed to the Variable x and y respectively i.e. in the
order of their position.
Note: If the number of Actual and Formal arguments are differ, than Python
gives an error.
2.Default Arguments:-
➢ Some times we can provide default values for our positional arguments. In
this case if we are not passing any value then default values will be
considered.
➢ Default argument must not followed by non-default arguments i.e.
the argument appears at the left side of the default argument must
have the default value.
def Interest (principle = 5000, Rate = 10.0 , Time = 3) :
def Interest (principle, Rate = 10.0 , Time = 3) : Valid
def Interest (principle, Rate , Time = 3) :
def Interest (principle = 6000, Rate = 10.0 , Time) :
Invalid
Example:
Note: Default arguments are also used the theory of positional arguments.
3.Keyword arguments:-
➢ The default keyword gives flexibility to specify default value for a
parameter so that it can be skipped in the function call,if need.However,still
we cannot change the order of arguments in function call i.e. you have to
remember the order of the arguments and pass the value accordingly.
➢ To get control and flexibility over the values sent as arguments,python offers
KEYWORD ARGUMENTS.
➢ This allows to call function with arguments in any order using name of the
arguments.
Example:
Example:
Rules for Combining All Four types of Arguments:
An argument list must first contain positional arguments followed by keyword
arguments.
Keyword arguments should be taken from the required arguments.
Youcannot specify avalue for an argument more than once.
Example 2:
Note:- The multiple values returned by return statement in the form of tuple.
Example:-
COMPOSITION
It refers to using an expression as a part oflarge expression, or a statement as a
part of large statement.
Example:
Max ((a+b),(b+c)) # ARITHMATIC
Prize(Cash or Card) #LOGICAL
NAME = “Vikram”
Print(NAME.replace(“m”, “nt”).upper()) # FUNCTION
SCOPE OF VARIABLES
➢ All variables in a program may not be accessible at all locations in that
program. It depends upon their declaration place inside the program.
➢ Scope of the variables refers to the part of the program where it is visible
i.e. area where you can use.
➢ There are two types of Scope of variables:-
a) Global:-
➢ variables declared at the top of the program or declared global in a
function.
➢ Variable defined outside all functions are global variables .
b)Local:-
➢ Variables declared inside a function definition or loop.
➢ A name declare in a function body is said to have local scope i.e. it can be used
only within this function and the other block inside the function.
➢ The formal parameters are also having local scope.
Example - 2
From the above example we can recall the concept learned in class XI
that Python variables are not storage containers, rather Python variables are
like memory references, they refer to memory address where the value is
stored,thus anychange in immutable type data will also change the
memory address. So any change to formal argument will not reflect
back to its corresponding actual argument and in case of mutable type,
any change in mutable type will notchangethe memoryaddressof
variable.
List is mutable i.e. data can be changed in the list
Example 1:
Because List is Mutable type, hence any change in formal
argument lst will not change the memory address, So changes
doneto lst will be reflected back to LIST1.
However if we formal ar GUMENT is assigned to some other variable or data
type then link will break and changes will not reflect back to ACTUAL ar GUMENT
Forexample(if insidefunctionupdateData()weassignmyListas:
myList = 20 OR myList = temp
Example 1:- Count how many Even and Odd numbers has entered.
Recursion
➢ It is one of the most powerful tool in programming language. It
is a process where function calls itself again and again.
➢ Recursion basically divides the big problem in to small problems up
to the point where it can be solved easily, for example if we have to
calculate factorial of a 5, we will divide factorial of 5 as
5*factorial(4), then 4*factorial(3), then 3*factorial(2), then
2*factorial(1) and now factorial of 1 can be easily solved
without any calculation,now each pending function will be
executed in reverse order.
Condition for implementing Recursion:-
➢ It must contain BASE CONDITION i.e. at which point recursion
will end otherwise it will become infinite.
➢ BASE CONDITION is specified using “if” to
specify the termination condition.
➢ Execution in Recursion is in reverse order using STACK. It first divide
the large problem into smaller units and then starts solving from
bottom to top.
➢ It takes more memory as compare to LOOP statement because
with every recursion call memory space is allocated for local
variables.
➢ The computer may run out of memory if recursion becomes infinite
or termination condition not specified.
➢ It is less efficient in terms of speed and execution time.
➢ Suitable for complex data structure problems like TREE,
GRAPH etc.