Python User Defined Function
A function is a sub program that acts on data and may or may not return a value. When same code is required to execute number of times, we can write a function for it and then call this function as many times as needed. Here you will learn Python user defined functions with examples-
Using user defined function is a two steps process-
1) Defining a Function
2)Invoking a Function or calling a function
1) Defining a Function
To defining a function we use def keyword to define a function.
Syntax-
def function name(<argument>):
<body of function>
2)Invoking a Function or calling a function
Once the function is defined we can call this function by the following statement. In this article we will see in details
Syntax
function();
fucntion(values);
Example- Write a program to find area of circle using user defined function .
How to define a function in Python
def tarea(b,h): #defining a function
area=(b*h)/2
print(“Area of triangle=”,area)
#main
b1=int(input(“enter base”))
h1=int(input(“enter base”))
tarea(b1,h1) #function calling or invoking
Output
enter base 4
enter base 5
Area of triangle= 10.0
Python library function examples
types of user-defined functions in python
There are the following list of user defined functions in python
1) Python function with argument and return value
2) Python function with argument and without return value
3) Python function without argument and without return value
4) Python function without argument and return value
1) Python function with argument and return value
This types of function takes values from the calling program and return value to the calling program.
Example
def sum(a,b):
c=a+b
return c
#main
a1=int(input(“enter the value of a”))
b1=int(input(“enter the value of b”))
print(“Sum of a and b=”,sum(a1,b1))
Output
enter the value of a 5
enter the value of b 4
Sum of a and b= 9
2) Python function with argument and without return value
This types of function takes values from the calling program and does not return value to the calling program.
Example
def sum(a,b):
c=a+b
print(“Sum of a and b=”,c)
#main
a1=int(input(“enter the value of a”))
b1=int(input(“enter the value of b”))
sum(a1,b1)
Output
enter the value of a 5
enter the value of b 6
Sum of a and b= 11
3) Python function without argument and without return value
This types of function does not takes values from the calling program and also does not return value to the calling program.
Example
def sum():
a=int(input(“enter the value of a”))
b=int(input(“enter the value of b”))
c=a+b
print(“Sum of a and b=”,c)
#main
sum()
Output
enter the value of a 9
enter the value of b 3
Sum of a and b= 1
4) Python function without argument and return value
This types of function does not takes values from the calling program but return value to the calling program.
Example
def sum():
a=int(input(“enter the value of a”))
b=int(input(“enter the value of b”))
c=a+b
return c
#main
print(“Sum of a and b=”,sum())
Output
enter the value of a 65
enter the value of b 43
Sum of a and b= 108
Python Named Arguments function
To have full control and flexibility over the order of values sent to the arguments Python provided another type of arguments called Keyword or Named arguments. In other words Keyword argument offer a way of writing function calls where we can write any value in any order provided we name the argument. Since in function call we write values along with argument name, we can write values in order. For example following are the valid of calling statements of function sum()
sum(y=8, x=6, z=2)
In the above written function call value 6 is going to x, value 8 is going to y and z will take value 2. This transfer values is irrespective of order of argument in function header.
sum(z=3, y=4, x=9)
In the above written function call value 9 is going to x, value 4 is going to y and z will take value 3. This transfer values is irrespective of order of argument in function header.
def test(a, b, c):
p=(a+b)/c
return p
#main
r=test(a=5, b=9, c=7)
print(“Result: “, r)
r=test(c=4, a=11, b=9)
print(“Result:”,r)
Python Default Arguments Functions
arguments which have some value in function header called Default arguments. While fnction call we can skip the value of default argument. For example-
def sum(x, y, z=4)
the above example of function header argument ‘z’ is having a value 4 assigned to it, so,argument ‘z’ is default argument. Now, while calling this function we can give values for only argument x and y; z will take 4 as its value.
We can even give values for all the three arguments also. In that case argument ‘z’ will take value provided by the user and ignore its default value. So we can say that in while calling function with default argument, the calling statement can have number of values less than or equals to the number of arguments in the function header. But, order of values has to be the same. For emple following are the valid example of calling statements of function sum()
sum(5,9,7)
In the above calling statement function argument x will take value 5, y will take value 9 and value 7 will be stored in z (‘z’ will ignore its default value 4).
sum(8, 2)
In the above calling statement function argument x will take value 8, y will take value 9 and z will
have its default value (4) as we have not exclusively provided value for z in calling statement.
Any argument, in a function header, cannot have default value unless all arguments on its right side have their default values. In other words only those arguments can have default values which are present in the right side of argument list. For example following is the list of some valid and invalid function header with default argument~
Valid Examples
sum(x, y, z=4)
sum(x,y=5, z=4)
sum(x=8, y=5, 2=4)
sum(x=3, y, z) sum(x,y=5, z) sum(x=8, y=5, z)
Invalid Examples
For Live Class Subscribe Our Youtube Channel by click on the below link
https://www.youtube.com/@olevelguruji
Write a program using user defined function with default argument. Calculate and print percentage of three subject marks. Use 300 as default value for maximum marks.
def percent(a, b, c, mm=300):
total=a+b+c
p=total*100/mm
return p
#main
print(“Percentage=”,percent(75,85,50)
print(“Percentage=”,percent(45,35,40,150)
python recursion functions
A Function calls it self again and again then it is called Recursion.
Example- python recursion factorial program
def factorial(x):
if x==1:
return 1
else:
return x*factorial(x-1)
#main program
num=int(input(“Enter a number:”))
fact=factorial(num)
print(“Factorial of number = “,fact)
Output
Enter a number:5
Factorial of number = 120
Thanks for reading the article Python user defined functions.
Also Check Our Latest Uploads