A Python program to perform arithmetic operations using functions.
In this post i am providing the example programs of different arithmetic operations such as Addition, Subtraction, Multiplication, Division and Modulo using functions.
The following program performs addition of two numbers by using functions. It is also being considered as an example program of user-defined functions.
Output
5+6= 11 |
Explanation
Initially we are defining the function with name add_numbers. Here we are passing two parameters inside the function are a and b. Next we are creating a variable ‘sum’ to store the result of a+b. The return statement returns the ‘sum’ result. We are defining two variables num1 and num2 and assign some value to them. Finally we can print the result by passing the two parameters num1 and num 2 to the function definition. After passing the values of num 1 and num 2 are allocated to a and b respectively and then the addition operation is performed and finally prints the result.
Similarly, we can code for subtraction,multiplication,division and modulo.
The following program performs subtraction of two numbers by using functions.
Output
20-10= 10 |
The following program performs multiplication of two numbers by using functions.
Output
7*6= 42 |
The following program performs division of two numbers by using functions.
Output
15/3= 5.0 |
The following program performs modulo of two numbers by using functions.
Output
10%4= 2 |