C++ Program to add two numbers using the function


In this program, You will learn how to add two numbers using the function in C++.


20 = 10 + 10

40 = 20 + 20

Example: How to add two numbers using the function in C++.

#include<iostream>
using namespace std;

int sum(int x, int y) {

   int z;
   z = x + y;
   return z;
}

int main() {

   int x, y, s;
   cout << "Enter 2 numbers:";
   cin >> x>>y;

   s = sum(x, y);
   cout << "Sum is:" << s;

   return 0;
}

Output:

Enter 2 numbers:10 20
Sum is:30