Add two numbers using function in c++

Add two numbers using function in c++ is simple addition program which we are implementing using functions in c++.

In this tutorial we will see what is function, How to add numbers using functions, and  c++ program to add two numbers.

What is function :  function is block of code.

How to add numbers using functions 

Addition of two numbers using function : First we declare function which adds the two numbers add(int, int), we define function by adding two numbers and we call function add() from main function by passing two numbers to function.

Algorithm to add two numbers using function in c++

  1. Start
  2. Define function which adds two numbers
  3. Write main function
  4. Take two number input
  5. Pass number and call function
  6. End

C++ addition program using function

12345678910111213141516#include <iostream>using namespace std;int addition (int a, int b){  int r;  r=a+b;  return r;}int main (){  int z;  z = addition (5,3);  cout << “The addition is = ” << z;}

Output :

C++ addition program using function
C++ addition program using function

Explanation :

1. Execution starts with the main function.

2. Declare variable

z => to store result

3. Call function to add numbers

addition (5,3);  => we pass 5 and 3 numbers to function , call function

4. Function executed

int addition (int a, int b) {  => addition(5,3)

int r; r=a+b;   => r= 8

return r;  => return 8 to fmain

}  => returning to main function

5. Now we print addition of number

cout << “The addition is = ” << z;

=>The addition is = 8