Jump to content

Pass by value/reference

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Termana

Termana

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,057 posts
First off, I will explain the difference between pass by value and pass by reference.

When you pass by value to a function (for example), it will copy the value stored in the variable you pass and if you change the copied variable it will not change the variable in the function that called the function.

However, when you pass by reference, you pass the address of variable, rather then the variable itself. So when you change the variable in the function, it WILL be changed in the function that called this function.

Since whenever you have probably used a function with parameters, I wont show how to pass by value, as that is what happens when you call a function with parameters. But I will show you the code to pass by reference and then explain it.


void TestFunction(int &variable1)

{

variable1 = 1337;

}


All I have done it put a & in front of the passed variable declaration. So if we had something like this:


#include <cstdlib>

#include <iostream>


using namespace std;


void TestFunction(int &variable1)

{

    variable1 = 1337;

}


int main(int argc, char *argv[])

{

    int a;

    a = 7;

    TestFunction(a);

    cout << a << endl;

    system("PAUSE");

    return EXIT_SUCCESS;

}


It will print the number 1337, because we called TestFunction, however if we A) Didn't call TestFunction or B) Did not put the & in front of variable1 - it would just print 7.

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#2
amrosama

amrosama

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 8,674 posts
nice and simple tutorial termana
+rep
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#3
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
This sentence basically fucks your entire tutorial.

Quote

However, when you pass by reference, you pass the address of variable, rather then the variable itself.

When you are talking about stuff like this, you need to make sure that you are extremely precise and explicit in what you say.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I have to agree with Methodz. Passing a pointer and passing a reference allow you to do the same thing, but the details are quite different. A reference (which you used in your examples) is not a pointer, but more of an alias.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog