C++ Program to add two numbers using call by Reference

In this program, You will learn how to add two numbers using call by Reference in C++.


Call by Reference

void sum(int a, int b, int *s) {
      //statement
}

C++ Program to add two numbers using call by Reference

Example: How to add two numbers using call by Reference in C++.

#include<iostream>
using namespace std;

void sum(int a, int b, int *s) {
    *s = a + b;
}

int main() {

    int a, b, s;

    cout << "Enter two numbers:";
    cin >> a>>b;

    sum(a, b, &s);
    cout << "Sum is:" << s;

    return 0;
}

Output:

Enter two numbers:10 20
Sum is:30