pass by value and reference

help me please

Teacher will give him tomorrow :""(


(Pass-by-Value vs. Pass-by-Reference)

Write a complete C++ program with the two alternate
functions specified below, each of which simply triples the variable count defined in main. Then
compare and contrast the two approaches. These two functions are
a) function tripleByValue that passes a copy of count by value, triples the copy and returns
the new value and
a) 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).
Do you know the difference between passing by value, and passing by reference?

If not, I suspect a Google search might be in order.
yah i know ..
but i don't have time ..Because i have quiz :"(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int tripleByValue(int num);
void tripleByReference(int &num);
int main()
{
	int num = 5;
	
	int result_by_val = tripleByValue(num);
	cout << "Return pass by value: " << result_by_val << endl;

	int result_by_ref = num;
	tripleByReference(result_by_ref);
	cout << "Return pass by ref: " << result_by_ref  << endl;

	return 0;
}

int tripleByValue(int num)
{
	return num*3;
}
void tripleByReference(int &num)
{
	num = num*3;
}
Last edited on
yah i know ..
but i don't have time ..Because i have quiz :"(


So, basically you can't be bothered to do your own homework when you can get someone else to do it for you. If you take that road you won't learn.
@swejnc:

Please don't do other peoples' homework for them. That's not what this site is for. The point of that homework is to help people learn how to do things for themselves, not so that they can not bother to do it till the last minute then get someone on the net to do it for them.
MikeyBoy:
OK~ I did this because the way I feel easier for me to learn is to have the solution first then I go figure it out. Unless he didn't bother to find out why and how the codes are done, otherwise I think he will learn it a faster way... I know many people will not agree with this ;D
You can generally tell by the nature of the post whether or not someone wants to learn or they just want to pass off a quick fix as their own.

I'd say it's definitely the latter in this case.
Topic archived. No new replies allowed.