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

Python Recursion

Uploaded by

dhruvtiwarix2910
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python Recursion

Uploaded by

dhruvtiwarix2910
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MOTHR TERESA SENIOR SECONDARY COED SCHOOL

SUBJECT – COMPUTER SCIENCE


NOTES CLASS XII CHAPTER – MORE ON FUNCTION

PARAMETER AND ARGUMENT IN FUNCTION


Parameter are the values provide in the parentheses when we write function of header.These are the value
required by function to work.
If there is more then one value is required by the function to work,then all of them will be listed as parameter
list seperated by comma.
For example:-
defs_i(p,r,t):
si=p*r*t
returnsi
In above example the function s_i() is use to calculate simple interset by passing value through function
calling,which term as argument.
An argument is a value that is passed to the function when it called.In other word argument are the values
provide in function call.
For example:-
s_i(12000,10,5)
Argument in python can be one of these value types-literals or variable or expression but parameters have to be
some name,i.e. variable to hold incoming
value
ACTUAL ARGUMENT AND FORMAL PARAMETER IN FUNCTION
Alternative name of argument is Actual Argument and the alternative name of parameter is formal parameter.
In other word the values provide in the parentheses when we write function of header. These are the value
required by function to work are called formal parameter.
For example:-def add(a,b): so a,b are the formal parameter.
The value that is passed to the function when it called.In other word the values provide in function call are
called Actual Argument.
For example:- add(10,15) so 10,15 are the actual argument
A few things to remember about parameter passing : In python function if you are passing value of immutable
type number,stringetc to the called function then function can not alter their value.But in case the passed value
in mutable in nature ,then called function would be able to make change .
Types of argument
1) Positional Argument
2) Default Argument
3) Keyword Argument
4) Variable Length Argument
Positional Argument:- Positional Argument are the argument passed to a function in a correct positional
order.
For example:-defsub(a,b):
print(a-b)
sub(100,200)
sub(200,100)
As shown in above example number and the position of the argument must be match
If we change the order ,then result will be change.
Default Argument- A Default Argument is an argument that assume a default value if a value is not provide
in the function call.we can provide default value for our positional argument.
For example:-defs_i(p,r,t=5):
si=p*r*t
returnsi
s=s_i(12000,10)
In given example if we are not passing three position argument during function call then only default argument
will be consider
Keyword Argument:-If there is a function with many parameter for such parameter can we provide value
using their name instead of position or order.These are called Keyword argument or named argument.
For example:-defs_i(p,r,t):
si=p*r*t
returnsi
s=s_i(p=12000,r=10,t=5)
s1=s_i(r=5,p=30000,t=10)
Advantage of Keyword argument are:-Using function with Keyword argument is esier as we do not need to
remember position or order of argument
Variable Length argument:- As name suggests, in certain situations,we can passed variable number of
arguments to a function.
Variable length argument are declared with * symbole in python
>>>deff1(*n)
For example:
def sum(*n):
total=0
for i in n:
total=total+I
print(“the sum=”,total) output: the sum=0
sum() the sum=20
sum(20) the sum=50
sum(20,30) the sum=100
sum(10,20,30,40)

SCOPE OF THE VARIABLES


All variable in program may not be access at all the location in the program.This depend on where you have
declared a variable.Scope of the variable refers to the part of the program where it is visible.We are study scope
of two type of variable global scope or local scope.
Global scope or global variable-The variable which declare top level or outside of any function body called
global variable. Global variable are used in all over the program
Local variable- The variable which declare inside the function body is called local variable. Accessesebility of
local variable only the inside the body of the function where they are declared.
For example:-
a=2 a is Global variable
def f(x):
y=x+ax,y are Local variable
return y
P=f(5)
print(P)
>>>7
a=2
def f(x,y):
a=x+y this ‘a’ has no knowledge of global ‘a’
return a
print(a)
>>>2
main() function
Some programming languages have a special function called main() which is the execution point for a program
file. Python interpreter, however, runs each line serially from the top of the file and has no explicit main()
function.
Including main() function in python program not mendatory.
The main function in Python acts as the point of execution for any program. Defining the main function in
Python programming is a necessity to start the execution of the program as it gets executed only when the
program is run directly and not executed when imported as a module.
For example:-
def Hello():
print(“HELLO,WORLD!”)
def main():
print(“this is main function”)
Hello()
main()
>>>this is main function
HELLO,WORLD!
In above example main() function is execution point for function Hello().

You might also like