Variations in User Defined Functions And Function Parameters
M3-R5.1 · Chapter 6: Functions · 8 min read
Variations in User Defined Functions
- Functions with no argument and no return value
- Functions with argument but no return value
- Functions with argument and return value
- Functions with no argument and but return value
|
def Add (x,y): print(“Addition =”,x+y) x=int(input(“Enter First number:”) y=int(input(“Enter Second number:”) Add(x,y) |
def Add (a,b): #Here a,b is parameter(formal) used in function Header print(“Addition =”,a+b) x=int(input(“Enter First number:”) y=int(input(“Enter Second number:”) Add(x,y) #Here x,y is Argument(Actual Parameter) used in function call statement |
||
|
def Add (a,b): print(“Addition =”,end=’ ‘) return (a+b) x=int(input(“Enter First number:”) y=int(input(“Enter Second number:”) r=Add(x,y) print(r) |
def Add (a,b): return (a+b) print(“Addition =”,end=’ ‘) x=int(input(“Enter First number:”) y=int(input(“Enter Second number:”) r=Add(x,y) print(r) |
def Add (): x=int(input(“Enter 1st number:”) y=int(input(“Enter 2nd number:”) return (x+y) r=Add() print(r) |
|
|
Functions with no argument and no return value def Add (): x=int(input(“Enter 1st number:”) y=int(input(“Enter 2nd number:”) sum=x+y print(“Addition=”,sum) Add() |
Functions with argument but no return value def Add (n1,n2): sum=n1+n2 print(“Addition=”,sum) x=int(input(“Enter 1st number:”) y=int(input(“Enter 2nd number:”) Add(x,y) |
||
|
Functions with no argument but return value def Add (): x=int(input(“Enter 1st number:”) y=int(input(“Enter 2nd number:”) sum=x+y return sum r=Add() print(“Addition=”,r) |
Functions with argument and return value def Add (n1,n2): sum=n1+n2 x=int(input(“Enter 1st number:”) y=int(input(“Enter 2nd number:”) r=Add(x,y) print(“The sum of value is=”,r) |
||
Points to remember……
- The return statement ends the function execution even if it is in the middle of the function. The statements written after return statement will not get executed
- If a function does not any value, by default it returns None
Example:
def greater(a,b):
if a>b:
return a
else:
return b
print(“Statement after return statement”)
x=int(input(“Enter 1st number:”)
y=int(input(“Enter 2nd number:”)
result=greater(x,y)
print(“greater Number is :”,result”)
Function Parameters: A function has two types of parameters:
1. Formal Parameter: Formal parameters are local variables which are assigned values from the arguments when the function is called.
2. Actual Parameter: When a function is called, the values that are passed in the call are called actual parameters. At the time of the call each actual parameter is assigned to the corresponding formal parameter in the function definition.
Difference Between Arguments and Parameters
|
Values being Passed are called arguments. |
Values being received are called parameters. |
|
Arguments appear in the function call statement. |
Parameters appears in Functions header or function definition |
|
Arguments are also known as actual Arguments and Actual parameters. |
Parameters are also known as Formal Parameters and formal Arguments |
Types of Parameters or Formal Arguments
We can call a function by using the following types of formal arguments:
- Required Arguments or Positional Arguments
- Keyword Arguments
- Default Arguments
- Variable-Length arguments
Note: This is the value which is supplied to the function to operate on
Required arguments: If a function has argument than we need to pass the values to function. These values are passed in a sequence and this sequence has to be followed strictly. Required arguments are the arguments passed to a function in correct positional order. Here the number of arguments in the function call should match exactly with the function definition.
|
def add(a,b): c=a+b print (“Addition:”,c) add(5,6) add() # Give Error |
def pw(x,y): z=x**y print z) pw(5,2) |
def pw(x,y): z=x**y print (z) pw(2,5) |
def pw(x,y): z=x**y print (z) pw(5,2,3) # Give Error |
Note: To call the function add ( ), we definitely need to pass two argument otherwise it would give a syntax error.
Keyword (Named) Arguments:
Sequence of the argument may be changed using the concept of keyword argument. These arguments are passed to the function with name value pair so keyword arguments can identify the formal argument by their names.
|
def interest(prince,rate,time): si=( prince*rate*time)/100 print("Simple Interest is=",si) interest(5000,10,2) # positional argument interest (time=2,prince=5000,rate=10) # keyword Argu. |
def fun(x,y): c=x+y print("result is =:",c) fun(x=87,y=90) fun(x=40,y=30) |
|
def team(name, project): print(name, "is working on a", project) team(name = “Python", project = ‘TCS') OUTPUT: Python is working on a TCS |
def team(name, project): print(name, "is working on an", project) team(project = “TCS", name = ‘Python') OUTPUT : TCS is working on an Python |
Default Arguments : Default argument has a default value. It will use in the absence of passing values at the time of calling. We use the = operator to provide default values.
|
def add ( a = 7, b = 8): sum = a + b print("Sum:", sum) add (2, 3) # function call with two arguments (output :05) add (a = 2) # function call with one argument(output :10) add () # function call with no arguments (output :15) |
def fun(x=10,y=30): c=x+y print("result is=:",c) fun(89,9) fun() |
|
def fun(x,y=30,z=8): # here y have default value c=x+y print(“result is=:”,c) fun(10) fun(10,20) fun(10,20,30) |
fun(x,y=30): # here y have default value fun(x,y=30,z): # give Error fun(x,y=30,z=0): fun(x,y=30,z=10,d): # give Error fun(x,y=30,z,d=20): # give Error |
Variable-length arguments: It is an argument that can accept any number of values. The variable length argument is written with * symbol. It stores all the value in a tuple.
|
def add(x,y): z=x+y print(“Additiion :”,z) add(5,2,4) TypeError: add() takes 2 positional arguments but 3 were given |
def add(*num): print(num[0]) print(num[1]) print(num[2]) add(5,2,4) |
def add(*num): z=num[0]+num[1]+num[2]+num[3] print("addition:",z) add(5,2,4,6) |
|
def add(a,*num): z=a+num[0]+num[1] print("addition:",z) add(5,2,4) |
def fun(*x): for i in x: print(i,end="") print() fun(1,2,3,4) fun(4,5) fun("vivek","sarita") |
|
NOTE: An Asterisk (*) Is placed befor the variable name that will hold the values of all non-kewword variable arguments
RECURSIVE FUNCTON(RECURSION):
Definition:
- A function calls itself, is called recursion.
- Recusation causes infinite execution of a function if we will be unable control the condition.
- We can use it as a loop but very carefully
|
def xyz(): print(“Hello”) xyz() xyz() |
def fact(n): if n==1: return 1 else: return n*fact(n-1) res=fact(5) print(res) |