C++ program to add two numbers using a function

In this article, you will learn a C++ program to add two numbers using a function. The following function takes two numbers as an argument from the user and returns the sum.

A function in C++ is a unique named block of code to perform a specific task. A good thing about the function is the reusability of the code. Create functions as many as you want and use them. We can use them in different programs having similar situations. For more understanding, you can check the article named user-defined functions in C++ language.

Example: C++ program to add two numbers using function

//C++ program to add two numbers using function
#include <iostream>
using namespace std;
//Function feclearation
int sumtwoNumber(int x,int y);

int main()
{
	
	int number1, number2,sum=0;
	

	cout<<"Enter first number: ";
	cin>>number1;
	cout<<"Enter second number: ";
	cin>>number2;
	
	sum=sumtwoNumber(number1,number2);
	cout<<"\nThe sum of "<<number1<<" and "<<number2<<" is: "<<sum<<endl;
	
	return 0;
}

//function to sum two numbers
int sumtwoNumber(int x,int y)
{
	return (x+y);
}

Output

cpp program to add two numbers using function

Recommended Articles

C++ Built-in Functions

C++ User-defined Functions

Scroll to Top