Write a C++ program to do the following: Define a function called \"difference\"
ID: 3691133 • Letter: W
Question
Write a C++ program to do the following:
Define a function called "difference" that can accept two integer values as arguments and returns an integer (which is the obtained by subtracting the first the second argument from the first argument.
Declare two variables in the main function and assign them values 7 and 10.
Call the difference function with the above created variables as arguments. Print the returned difference in the main function.
Call the function again with 11 and 5 as arguments and print the obtained difference.
Print the two differences on separate lines.
Explanation / Answer
/*
* File: main.cpp
* Author: chegg
*
* Created on April 19, 2016, 1:47 AM
*/
#include <iostream>
using namespace std;
int difference(int, int);
// main function
int main(){
// define some variable here
int first, second, result;
//assign value
first = 7;
second = 10;
// call difference function and get result
result = difference(first,second);
// print first result here
cout << "Differences between "<< first <<" and " << second <<" is : " << result;
//assign value again
first = 11;
second = 5;
// call difference function again and get result
result = difference(first,second);
// print second result here
cout << " Differences between "<< first <<" and " << second <<" is : " << result;
return 0;
}
// define difference function
int difference(int first, int second){
// subtracting and return value
return first - second;
}
Output:-
Differences between 7 and 10 is : -3
Differences between 11 and 5 is : 6
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.