Problem: Write a C++ program that has a function
called add5. The main function
prompts the user for a number and calls function add5. add5
increments the number by 5 and prints the result.
#include <iostream>
using namespace std;
void add5 (int n);
void
main( )
{
int num;
cout
<< "Please enter a value: ";
cin
>> num;
add5 (num);
}
//Input: integer Output: the integer incremented by 5
void
add5 (int
n)
{
n = n + 5;
cout
<< "Five added to " << n << " is "
<< n2 << endl;
}
add5 is a function with one input value and no output value.
What
happens in memory?
(a)When
function add5 is called, memory is allocated for formal parameter
n.
(b)
A copy of the value in num is placed in this memory location. When add5 finishes, the memory allocated to n
is then deallocated.
n is called a value parameter.