×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to add two integer numbers using function

Given two integer numbers, we have to add them using functions in C++.
[Last updated : February 28, 2023]

Adding two integer numbers using function

In the last program [C++ program to add two integer numbers], we discussed how to take input and find the sum of two integer numbers?

In this program we are doing the same but using a user defined function, this program will take two integer numbers are calculate the sum/addition of them using a user defined function.

The function addition is used to calculate addition of the numbers, in this program we will pass the entered integer numbers and function will return the addition of the numbers.

Program to add two numbers using function in C++

#include <iostream>
using namespace std;

//function declaration
int addition(int a, int b);

int main()
{
    int num1; //to store first number
    int num2; //to store second number
    int add; //to store addition

    //read numbers
    cout << "Enter first number: ";
    cin >> num1;
    cout << "Enter second number: ";
    cin >> num2;

    //call function
    add = addition(num1, num2);

    //print addition
    cout << "Addition is: " << add << endl;

    return 0;
}

//function definition
int addition(int a, int b)
{
    return (a + b);
}

Output

Enter first number: 100 
Enter second number: 200
Addition is: 300

Function - addition()

Function has following parameters and return type

  • int a - first integer number
  • int b - second integer number
  • return type int - function will return an integer value, that will be the sum of a and b

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.