[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Call by Reference vs Call by Value C++

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 21
Thread images: 2

File: download.jpg (5KB, 288x154px) Image search: [Google]
download.jpg
5KB, 288x154px
Hey guys, I have an exam coming up soon in C++ and I am not quite sure of the difference between call by reference vs call by value in C++

Can anyone explain this to me?
>>
>>57572063

When you pass a value to a parameter the function creates a copy and uses its copy to do whatever it needs to do and then disposes of the copy or returns one and one value only.

Pass by reference is good for when you need to return multiple values. The values that are passed by reference are not copied but rather a direct link is made so any changes that occur to them in the function are actually made to the original value.
>>
So what is the meaning of the little "&" symbol in reference to this whole thing? I don't quite understand what that means
>>
>>57572148

The ampersand is put next to the data type of the parameter to let the system know that values passed to that parameter are going to be pass by reference.

It is known as the "reference operator"
>>
thank you very much. I may just actually ace this test.
>>
File: 1454909533365.png (55KB, 571x553px) Image search: [Google]
1454909533365.png
55KB, 571x553px
>>57572063

Pass by Reference
void passThisShitNigga(blunt& weedShit)

will pass in the memory address of the variable/object being passed in for it to be directly manipulate. So if you pass a variable in from main, when the function ends, the variable in main will be altered.

Pass by value
void copyThatBluntDawg(blunt shittyWeed)

will create a copy of the value stored in that variable, and use it in the function. So if you pass a variable from main, when the function ends, the variable in main will be unaffected
>>
u deserve to fail
>>
>>57572482
A reference is just a pointer.
>>
>>57572063
By val passes the item on the stack (aka a copy of it).

By ref passes the memory location, and then the function works on the data at that memory address(es)

By ref is good for large data (like an array) or if you want multiple return values.
>>
>>57573289
Not entirely true. The way they are implemented is identical, but cannot be used the same way.

Pointer can be null. Reference cannot. Also reference arithmetic does not exist.

In general you should pass by reference over pointer, unless you need to do more than what a reference offers.
>>
>>57572063
Nope, you deserve to flunk out of college you numale liberal arts cuck.
>>
Call by reference is implicitly sending the reference of an object
>>
>>57573758
>Pointer can be null. Reference cannot. Also reference arithmetic does not exist.
You said that some pointer are not references. I agree. Please could you read carefully my initial post?
>>
>>57573289
a reference is the type of a dereferenced pointer. but it's more generally the type of an lvalue or rvalue, that is, storage for a value. this distinction matters for example when talking about temporary objects where the "memory address" may only be conceptual and is ultimately ellided by the compiler
>>
>>57573852
That's how I spot code monkeys.
>>
Call by reference is a syntactic sugar over call by pointer. Consider the following:

int foo = 2;
int& bar = foo; // bar is a reference to foo
bar = 3; // foo is now 3


Equivalent to:
int foo = 2;
int *bar = &foo; // bar is a pointer to foo
*bar = 3; // foo is now 3


Unlike a pointer, you can't reassign a reference to another address (although a function that takes reference parameters can always be given different values -- it isn't static), and you can't use pointer arithmetic with a reference to grab the next element. This has the general advantage of ensuring values to be legitimate, absent intentional stupidity.

void add_one(int& x)
{
x += 1;
}

int main(void)
{
int foo = 2;
int *bar = NULL;
add_one(foo); // This sets foo to 3
add_one(NULL); // This isn't valid
add_one(*bar); // But this is, and it will crash
return 0;
}
>>
>>57573790
Key point being implicit. This is why c and Java are strictly pass by value. You can simulate pass by reference, but it is not the same.
>>
>>57573892
a reference is just the type of a dereferenced pointer. it can't be null for the same reason you can't dereference null. the last example compiles because it passes the typecheck that dereferenced bar is a reference, but it crashes because bar turns out to be null at runtime and dereferencing null is a segmentation fault
>>
>>57573892
>add_one(*bar); // But this is, and it will crash
It's not valid though. It compiles, but it's undefined behaviour.
>>
>>57572148
just like C, C++ uses operators in both declarations and in distinct separate but related operations.

depending on the context, & can mean either a variable reference or to take the address of a variable and return a pointer to it.

a pretty retarded design choice in retrospect.
>>
>>57572063
Pass by value gives you the value of the variable. Pass by reference gives you the location in memory of the variable.
Thread posts: 21
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.