Functions with arguments and returning value
In this we pass arguments to the function and function also returns a value.
Examples:
Question:1. Python program to calculate square of a number.(pass and return)
#function definition def square(a): sq=a*a return(sq) #function calling n=int(input('enter any number: ')) s=square(n) print('square of ',n,' =',s)
Output:
enter any number: 7
square of 7 = 49
>>>
Question:2. Python program to calculate the cube of a number. (pass and return)
#function definition def cube(a): cu=a*a*a return(cu) #function calling n=int(input('enter any number: ')) c=cube(n) print('cube of ',n,' =',c)
Output:
enter any number: 6
cube of 6 = 216
>>>
Question:3. Python program to calculate the average of three numbers.(pass and return)
#function definition def avrg(a,b,c): av=(a+b+c)/3 return(av) #function calling m=int(input('enter 1st number: ')) n=int(input('enter 2nd number: ')) o=int(input('enter 3rd number: ')) A=avrg(m,n,o) print('Average of three numbers= ',A)
Output:
enter 1st number: 4
enter 2nd number: 5
enter 3rd number: 6
Average of three numbers= 5.0
>>>
Question:4. Python program to calculate area of rectangle.(pass and return)
#function definition def rec_area(l,b): ar=l*b return(ar) #function calling l=int(input('enter length of a rectangle: ')) b=int(input('enter breadth of a rectangle: ')) Area=rec_area(l,b) print('Area of a rectangle = ',Area)
Output:
enter length of a rectangle: 20
enter breadth of a rectangle: 50
Area of a rectangle = 1000
>>>
Question:5. Python program to maximum of three numbers.(pass and return)
#function definition def large(a,b,c): m=0 if(a>b and a>c): m=a else: if(b>c): m=b else: m=c return(m) #function calling a=int(input('enter 1st number: ')) b=int(input('enter 2nd number: ')) c=int(input('enter 3rd number: ')) ma=large(a,b,c) print('largest of three numbers = ',ma)
Output:
enter 1st number: 20
enter 2nd number: 45
enter 3rd number: 15
largest of three numbers = 45
>>>