Python Program To Add Two Numbers Using Function

Python Program To Add Two Numbers Using Function

In this tutorial, you will learn to write a Python Program To Add Two Numbers Using Function.

To understand this program you should have an understanding of the following topics:

  1. Python Functions

Python Program To Add Two Numbers Using Function

Following is the code for a python program to add two numbers using functions.

def add_numbers(a, b):
    return a + b

num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

result = add_numbers(num1, num2)

print("The sum of", num1, "and", num2, "is", result)

Output

Enter first number: 3
Enter second number: 4
The sum of 3.0 and 4.0 is 7.0

In this program, we define a function add_numbers that takes two arguments a and b, and returns their sum.

We then prompt the user to enter two numbers and store them in the variables num1 and num2.

We call the add_numbers function with num1 and num2 as arguments and store the result in the variable result.

Finally, we print out a message that displays the sum of the two numbers.

Was this helpful?
YesNo

Related Articles:

Recent Articles:

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x