can anyone explain to me what is the definition for the both above.i had search on the internet but still cant got wat it is mean by.if can pls show some example to me.thanks

can anyone explain to me what is the definition for the both above.i had search on the internet but still cant got wat it is mean by.if can pls show some example to me.thanks

Basically, call-by-value creates a local copy of the variable for use inside the function, whereas call-by-reference enables the function to have access to the original variable. A simple demonstration would be:

#include <iostream>

void func_1(int iNumber);
void func_2(int& iNumber);
void func_3(int* piNumber);

int main()
{
    int x = 1;

    // Use the pass-by-value function
    std::cout << "Before func_1, " << x << std::endl;
    func_1(x);
    std::cout << "After  func_1, " << x << std::endl;

    // use the reference pass-by-reference function
    std::cout << "Before func_2, " << x << std::endl;
    func_2(x);
    std::cout << "After  func_2, " << x << std::endl;

    // Make a pointer to the number to pass to func_3
    int* p;

    // aim the pointer at the variable
    p = &x;

    // use the pointer pass-by-reference function
    std::cout << "Before func_3, " << *p << std::endl;
    func_3(p);
    std::cout << "After  func_3, " << *p << std::endl;
}

void func_1( int iNum ){
    std::cout << "\tStart of func_1, " << iNum << std::endl;
    // This is a local copy of the variable that you passed, it will be destroyed after the function exits
    iNum++;
    std::cout << "\tEnd of func_1,   " << iNum << std::endl;
}

void func_2( int& iNum ){
    std::cout << "\tStart of func_2, " << iNum << std::endl;
    // A reference was passed, so this is changing the actual variable, not working with a copy
    iNum++;
    std::cout << "\tEnd of func_2,   " << iNum << std::endl;
}

void func_3( int* piNum ){
    std::cout << "\tStart of func_3, " << *piNum << std::endl;
    // This is a pointer to the original value, so you have to dereference it with the '*' operator
    *piNum = *piNum + 1;
    std::cout << "\tEnd of func_3,   " << *piNum << std::endl;
}

If he's asking about parameter passing, he's still a newbie at programming..... I doubt the code will help......

Imagine it like this.....

PASSING BY VALUE:- Someone gives you a piece of paper containing a program. You take it to a photocopy shop and xerox it. Then you take the photocopy and use it as your own.

In this form the value is sent but the original value is not accessible.


PASSING BY REFERENCE:- Someone tells you where the program is stored on the computer and you search for it and use that original program directly.

In this form the memory location of the actual value is given so the original value can easily be accessed.

erm, the question is
Function tripeCallByValue that passes a copy of count call-by-value,triple the copy and return to a new value.does that means,

int tripeCallByValue(int count){
count=count*3;
return new_value;
}

In your code, you are returning something called new_value. What is that? It does not exist. Return the value you want to return, not some made up value that does not exist.

Your terminology is wrong, that is why your internet searches on the subject fails to yield anything. The correct term is "passed-by-value" and "passed-by-reference", i.e. "passed" not "call". Call-by-value makes no sense (those who use this terminology do so incorrectly).

In lay terms (similar to GaidinDaishan):
Imagine you are one worker ("function") and your friend is another worker ("function"). Your friend wants you to do something ("call" upon you), but you need a password to accomplish your task (a "parameter" needs to be "passed"). Your friend has the password written on a piece of paper ("variable"). Now, there are two options:

Pass-by-value: You could have your own piece of paper ("local variable") for your friend to write the password on ("copy" its "value"). In this case, you can accomplish your task and your friend preserves the original, unaltered and, more importantly, unalterable.

Pass-by-reference: Your friend could simply pass-on the piece of paper to you. This is cheaper since no-one has to waste time writing a copy. But, now, you hold the original paper with the password, and you can do whatever you like with it (which can be useful, or not). This can be useful if you happen to change the password, then you can just erase the old password on the paper and replace it by the new one, and when you are finished, you hand the paper back to your friend and thus informing him of the new password. This is, however, not useful if you just need that password ("value") initially, but then, you would like to use the piece of paper ("variable") to scramble other things on it without screwing up your friend's precious original password record (in which case pass-by-value is preferred).

It's really as simple as that. And there isn't a whole lot more to it than that (except for cv-qualifiers).

Extra: The other popular and important option is passed by const reference. Which, with the above analogy, means that your friend passes the original piece of paper on to you (without the burden of copy-making), but you give him a promise that you will not change what is written on that paper (and this is a "real" promise that the compiler forces you to keep, this is the basis of "const-correctness").

Edited 5 Years Ago by mike_2000_17: n/a

@.@ still not understanding.can some1 show the example based on the question that i post above.

int CallByValue(int x)
{
x = 2;
}

int main()
{
int x = 0;
 
cout<<"Before CallByValue:"<<x<<endl;
CallByValue(x);
cout<<"After CallByValue:"<<x<<endl;

is that something like this?

This article has been dead for over six months. Start a new discussion instead.