In this tutorial, we will learn a simple concept of how to add two numbers using the function in the Python programming language.
Add two integer number using the function
1 2 3 4 5 6 7 8 9 |
#Python program to add two numbers using function def add_num(a,b):#function for addition sum=a+b; return sum; #return value num1=45 #variable declaration num2=55 print("The sum is",add_num(num1,num2))#call the function |
Output:
The sum is 100
Add two integer number using the function – get input from the user
This is the program asked input from user two numbers and displays the sum of two numbers entered by the user
We can use pre-defined python function input() to takes input from the user. Input() function returns a string value. So we can use int() function to convert from string to int data type (shown in line 6 and 7).
1 2 3 4 5 6 7 8 9 |
#Python program to add two numbers using function def add_num(a,b):#function for addition sum=a+b; return sum; #return value num1=int(input("Enter First Number: "))#input from user for num1 num2=int(input("Enter Second Number :"))#input from user for num2 print("The sum is",add_num(num1,num2))#call te function |
When the above code is compiled and executed, it produces the following results