Python | A function that accepts 2 integers and adds them and returns their sum

Here we will go through two methods that are there to solve this problem. Let us take a look at each of them.

Method 1: Using function by passing arguments (int)

def add_two_num(a,b):
    sum=a+b;
    return sum; 
    
num1=int(input("Input the first number : "))
num2=int(input("Input the second number  :"))

print("The sum of given two numbers is",add_two_num(num1,num2))

Output:

def add_two_num(a,b):
    sum=a+b;
    return sum; 
num1=int(input("Input the first number : "))
num2=int(input("Input the second number  :"))
print("The sum of given two numbers is",add_two_num(num1,num2))

Explanation:

Simply defined the function that accepts two arguments. Used the + operator to add those two numbers and return the value stored in the sum variable. We used the input() function to accept the input from the user.

Method 2: Using function by passing arguments (float)

def add_two_num(a,b):
    sum=float(a)+float(b);
    return sum; 
    
num1=input("Input the first number : ")
num2=input("Input the second number  :")

print("The sum of given two numbers is",add_two_num(num1,num2))

Output:

Input the first number : 12.365
Input the second number  :12.563
The sum of given two numbers is 24.928

Here we simply used the float type so that we can process the decimal values and provide the answer in decimal.

Here is the end of our article. Try exploring other methods and feel free to share.

Share:
Avatar of Dhvanil V

Author: Dhvanil V

Leave a Reply

Your email address will not be published. Required fields are marked *