Call-by-copy: What is it?

DEATH eVADER

Space Core
Joined
Nov 10, 2003
Messages
8,142
Reaction score
19
Self explanatory title,

What is it?
What does it do?

I was also wondering if anybody could give me examples of where it is used (Code included)

Soz in advance for being too upfront, but it would be helpful getting advice from the community on where to implement it
 
I'm assuming you mean call-by-value? It's the "standard" way of passing objects to a function.

It copies the given argument to a newly allocated, local object. This means that if you change its value in the function, this change will not be reflected in the original object.

Example:
Code:
void somefunction( int foo )
{
    foo = 25;
    cout << foo << endl;  //will print: 25
}

int main ()
{
   int foo = 5;
   cout << foo << endl;    //will print: 5
   somefunction( foo );
   cout << foo << endl;    //will print: 5
}

An alternative is call-by-reference, which will not copy the object, and will let you modify the original one inside the function.

Example to illustrate difference:
Code:
void somefunction( int &foo )  //note the ampersand, means it's a reference
{
    foo = 25;
    cout << foo << endl;  //will print: 25
}

int main ()
{
   int foo = 5;
   cout << foo << endl;    //will print: 5
   somefunction( foo );
   cout << foo << endl;    //will print: 25
}

Where is it used... well, everywhere. Perhaps it's better to ask where it isn't used, which is, to name some: when you do want to change the original object's value, or when the object is large and you don't want to copy it around for performance reasons.
 
cheers, its actually easier than I first believed, I think I've already included this in my assignment. But yeah, I just noticed the ampersand, I think that was what I was doing wrong before. Thanks for the reply.

I take it the ampersand is always required , regardless of where you put your function (Before or After main code)

The problem is that there is perhaps only one tutor who calls call-by-reference, call-by-copy.
 
Seeing as you have to define a function before calling it, placing the function after main() will simply not work. Besides that, where you define your function does not influence its internal workings. The ampersand is indeed required as it denotes a parameter as being a reference.

It's weird that one would call call-by-reference call-by-copy, because the point of it is that no copying of the arguments occurs.

There's quite a lot of documentation on c++ on the net as you may know, also on this particular subject. It might be helpful in sorting out what is what.
 
Wouldn't you just have to define void function(); before main? Then the actual code of the function be defined before or after?:
void function();
int main()
{
return 1;
}
void function()
{

}

I guess it really depend on your compiler although defining my functions has always been one of the first things I do.
 
That'll work, I'm just used to keeping it simple and putting them above main. In the context here it also seems like an unnecessary complication unless death evader already knows about forward declaration.
 
Back
Top