Write two alternate functions specified below, each of which simply triples the variable count defined in main. These two functions are: a. Function tripleByValue that passes a copy of count by value, triples the copy and returns the new value.b. Function tripleByReference that passes count by reference via a reference parameter and triples the original value of count through its alias(i.e. the reference parameter)For example, if count

Sagot :

Answer:

Following are the code to this question:

#include <iostream>//header file

using namespace std;

int triplebyValue(int count)//defining a method triplebyValue

{

int x=count*3;//defining a variable x that multiply by 3 in the count  

return x;//return value of x

}

void triplebyReference(int& count)//defining a method triplebyReference that hold count variable as a reference in parameter

{

count*=3;//multipling a value 3 in the count variable

}

int main()//main method

{

int count;//defining integer variable

count=triplebyValue(3);//use count to call triplebyValue method

cout<<"After call by value, count= "<<count<<endl;//print count value with message

triplebyReference(count);//calling a method triplebyReference

cout<<"After call by reference, count= "<<count<<endl;//print count value with message

return 0;

}

Output:

After call by value, count= 9

After call by reference, count= 27

Explanation:

In this code two methods "triplebyValue and triplebyReference" are declared, which accepts a count variable as a parameter, and in both multiply the count value by 3 and return its value.

Inside the main method, an integer variable "count" is declared that first calls the "triplebyValue" method and holds its value into count variable and in the next, the method value is a pass in another method that is "triplebyReference", and use print method to print both methods value with a message.