Home » C++ programs » C++ Most popular & searched programs

C++ program to add 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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.