Skip to content

Parameters or Arguements

Values to be passed by the caller function at the time of invocation.

 

2 types of parameters:
Positional Parameters : those appear within parenthesis inside function header
Actual Parameters: those appear inside function invocation.

 

positional parameters must be variables, actual parameters can be numbers or variables.

🐍
filename.py
#example of function not accepting arguments
def test1():
    print("Hi")
    return

#example of function accepting arguments
def test2(x):  #x is formal argument
    print(x)
    return

#main program
test1()
test2(5) #5 is actual argument

4 types of function definitions can be:
1. no args; no return value
2. no args; with ret value
3. with args; no ret val
4. with args; with ret val

🐍
filename.py
#Example
#no args; no return value
def add():
    x=5
    y=6
    print("sum=",x+y)
    return

#main program
add()
🐍
filename.py
#Example
#no args; with return value
def add():
    x=5
    y=6
    return x+y

#main program
res=add()
print("sum=",res)
🐍
filename.py
#Example
#with args; no return value
def add(x,y):
    print("sum=",x+y)
    return

#main program
add(5,6)
🐍
filename.py
#Example
#with args; with return value
def add(x,y):
    return x+y

#main program
res=add(5,6)
print("sum=",res)

Practice Question:
Write Area of Circle program using four types of function definitions

>>>Types of Arguments
In python, we cannot specify what type of argument that a function can take that is already defined.

>>>Default Arguments
default values assigned to formal arguments that are used in the absence of actual arguments.

🐍
filename.py
#Example
def add(x=0):
    print(x)
    return

#main program
add()
add(34)

Non default arguments must be appear before default arguments

🐍
filename.py
#Example
def test(a,x=0,y=0.0):
    print(a,x,y)    
    return

test(5)
test(15,6.7)

>>>Keyword Arguments
The values directly assigned to formal arguments in the invocation of the function, in order to not maintain correct assignment of formal arguments .

🐍
filename.py
#Example
def test(fname,lname):
    print("Hi",fname,lname,"welcome")
    return

#main program
test(lname="Kumar",fname="Anil")

We can pass multipe values to single formal argument if it is defined like below:

🐍
filename.py
def funcName(*var):
	.....
🐍
filename.py
#Example
def add(*x):
    print(x[0])
    print(x[1])
    print(x[2])
    return

#main program
add(1,3,5)

Practice Questions:
1. Define a function greet() that prints “Hello, Python!” and invoke it.
2. Write a function add(a, b) that returns the sum of two numbers. Call it with 10 and 20.
3. Create a function is_even(n) that returns True if the number is even, else False.
4. Write a function area_of_circle(radius) that returns the area. Use π = 3.14.
5. Create a function max_of_three(a, b, c) to return the maximum of three numbers.
6. Define a function square_list(numbers) that returns a new list with squares of each element.
7. Create a function divide(a, b) that returns a/b. Call it with values 10, 2.
8. Write a function student_details(name, age) and call it using keyword arguments.
9. Create a function power(base, exponent=2) that returns base**exponent.
10. Write a function total_marks(*marks) that calculates and returns the sum of any number of marks.
11. Create a function display_info(**info) that prints key-value pairs passed as keyword arguments.