PREV NEXT
What is Functions?
- Function is nothing but collection of statements to perform a specific task
- By using functions we divided large programs as multiple modules
- It is help to debug the code easily
- Modification of code became very easy
- Depend of statements will be decrease
- Repetation of same set of statement code will be avoided
Syntax:
def function_name(arg1,arg2……..argn):
”’doc string”’
statement 1
statement 2
……………..
……………..
……………..
statement n
return statement
A function contains two parts , header and body
Header:
- Header is start with ‘def’ keyword and followed by nae of the fucntion,arguments and finally haveing the ‘:’ to tell the compiler function defination has been completed
- def is a reserved keyword,funcion name should be a any valid identifier,here argument is optional,it is depend on the function implementation.
- Here docstring also optional
Body:
- Body is tell to the function what should does
- return statement is an optional,if caller is required to get any return value from the defination then we can use the return statement
1. User defined function without arguments:
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
def calculation():#this is fuction defination a=int(input("Enter a number:")) b=int(input("Enter a number:")) print("addition:",a+b) print("substraction:",a-b) print("multiplication:",a*b) print("division:",a/b) print("Modulus:",a%b) calculation()#this is function call |
Input:
1 2 3 |
Enter a number:3 Enter a number:2 |
Output:
1 2 3 4 5 6 7 8 9 |
addition: 5 substraction: 1 multiplication: 6 division: 1.5 Modulus: 1 |
2. User defined function with arguments:
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
def calculation(a,b):#this is fuction defination print("addition:",a+b) print("substraction:",a-b) print("multiplication:",a*b) print("division:",a/b) print("Modulus:",a%b) a=int(input("Enter a number:")) b=int(input("Enter a number:")) calculation(a,b)#this is function call |
Input:
1 2 3 |
Enter a number:3 Enter a number:2 |
Output:
1 2 3 4 5 6 7 8 9 |
addition: 5 substraction: 1 multiplication: 6 division: 1.5 Modulus: 1 |
Return Statement:
The return statement is used to quite the fucntion and recah back to where it was called
syntax:
return statement
How to use return single value:
Example:
1 2 3 4 5 6 7 8 9 10 11 |
def sum(a,b):#this is fuction defination return a+b a=int(input("Enter a number:")) b=int(input("Enter a number:")) res=sum(a,b)#this is function call print("Res is :",res) |
Input:
1 2 3 |
Enter a number:3 Enter a number:2 |
Output:
1 |
Res is : 5 |
Explanation:
1. Defined a fucntion with name of sum with arguments
2. read the values a,b
3. called that function with a,b values
4. once invoked the function
5. it will perform addition between a,b and it will return the result to caller
6. The returned value we are storing res variable
7. After print res it will print the result
How to use return multiple values:
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
def calculation(a,b):#this is fuction defination add=a+b sub=a-b mul=a*b return add,sub,mul a=int(input("Enter a number:")) b=int(input("Enter a number:")) add,sub,mul=calculation(a,b)#this is function call print("Addition is :",add) print("Substraction is :",sub) print("Multiplication is :",mul) |
Input:
1 2 3 |
Enter a number:3 Enter a number:2 |
Output:
1 2 3 4 5 |
Addition is : 5 Substraction is : 1 Multiplication is : 6 |
Scope and life time of variable:
Scope:
scope of the variable is nothing but part of the program where it can be accessed
Life Time of the Variable:
Life time of the variable is nothing but period of the variable through out it exists
There is two types of variables are available.They are
1. Local Variable
2. Global Variable
1. Local Variables:
The local variable is nothing but whichever the variable we can create inside the function defination we can call it as Local variables.It can be accessble and life time of the variable within the fucntion only
2. Global Variables:
The global variable is nothing but whichever the variable we can create ouside the function defination we can call it as Global variables.It can be accessble and life time of the variable end of the program
Example 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
glo_var=50 def fun(): loc_var=40 print ("Inside Fun:Global variable is:",glo_var) print ("Inside Fun Local variable is:",loc_var) fun() print ("Outside Fun:Global variable is:",glo_var) |
Output:
1 2 3 4 5 |
Inside Fun:Global variable is: 50 Inside Fun Local variable is: 40 Outside Fun:Global variable is: 50 |
Example 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
glo_var=50 def fun(): loc_var=40 print ("Inside Fun:Global variable is:",glo_var) print ("Inside Fun Local variable is:",loc_var) fun() print ("Outside Fun:Global variable is:",glo_var) print ("Outside Fun Local variable is:",loc_var) |
Output:
1 2 3 4 5 |
Inside Fun:Global variable is: 50 Inside Fun Local variable is: 40 Outside Fun:Global variable is: 50 |
Traceback (most recent call last):
File “fun5.py”, line 8, in <module>
print (“Outside Fun Local variable is:”,loc_var)
NameError: name ‘loc_var’ is not defined
Note: If we observe above example ,i am trying to access the local variable outside of the function,after execute got is not defined ,because of it is a local variable .
Example 3:
1 2 3 4 5 6 7 8 9 10 11 |
var="Global" def fun(): var="Local" print("Inside Fun:Value of the variable is :",var) fun() print("Outside Fun:Value of the variable is :",var) |
Output:
1 2 3 |
Inside Fun:Value of the variable is : Local Outside Fun:Value of the variable is : Global |
Default Argument or Default Parameters:
Default arguments is nothing but when ever we are defining the fucntion at the time only we can intialize the variable to that function defination and whenever we invoke the function without any arguments that time it will take from the already assigned values to arguments inside the defination
Syntax:
def fun_name(arg=value1,arg2=value2……..argn=valuen):
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
def default_arg_fun(a=10,b=20,c=30): print("Value a is:",a) print("Value b is:",b) print("Value c is:",c) x=1 y=2 z=3 print("Calling without Args") default_arg_fun() print("Calling with single Arg") default_arg_fun(x) print("Calling with two Args") default_arg_fun(x,y) print("Calling with two Args") default_arg_fun(x,z) print("Calling with three Args") default_arg_fun(x,y,z) |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
Calling without Args Value a is: 10 Value b is: 20 Value c is: 30 Calling with single Arg Value a is: 1 Value b is: 20 Value c is: 30 Calling with two Args Value a is: 1 Value b is: 2 Value c is: 30 Calling with two Args Value a is: 1 Value b is: 3 Value c is: 30 Calling with three Args Value a is: 1 Value b is: 2 Value c is: 3 |