Simple Question

Is call by value parameter cannot be changed by a function call?
Please, give an example and/or a more detailed explanation.
An arguement corresponding to a(n) ____________ parameter cannot be changed by a function call.
a) call-by-reference
b) call-by-value
c) object
d) class
Run this little tidbit and you'll find out the answer in no time.
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
28
29
30
31
// Example program
#include <iostream>
#include <string>

int passValue( int stuff );
int passRef( int &stuff );

int main()
{
    int a = 5;
    int b = 10;
    std::cout << "Original values of a and b are " << a << ", " << b << std::endl;
    
    passValue(a);
    std::cout << "New value of a is " << a << std::endl;
    
    passRef(b);
    std::cout << "New value of b is " << b << std::endl;
}

int passValue( int stuff )
{
    stuff += 5;
    return stuff;
}

int passRef( int &stuff )
{
    stuff *= 2;
    return stuff;
}
so the answer is call by value right??=)
Short answer: I dunno.

Long answer: I dunno, what do you think it is after seeing what the functions did to each of the variables a and b?
http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value
Get it! So matter what the value cannot change if i didnt put "&" at (int &stuff) right??
#include <iostream>

using namespace std;

void xx (int &x)
{
x = 20;
}
void yy (int x)
{
x = 20;
}

int main()
{
int x = 10 ;
cout << x << endl;
yy(x);
cout << x << endl;
xx(x);
cout << x << endl;
}

Correct. Basically the difference between passing by value and passing by reference is this:

In passing by value, you're creating another copy of the variable to be changed. Think of two hamster cages connected by one tube. Box A on the left (if you can picture it in your mind) is the function definition. Box B on the right is the call to the function in main(). When you pass by value, you create another hamster. So Box A has one hamster that does its thing, and Box B has another that's the exact same thing.

In passing by reference, imagine the same two hamster cages, and still the one hamster in Box A. When a passing by reference function gets called, the one hamster in Box A runs straight through the tube to Box B and because of that, it's tired. So instead of an ok hamster in Box A, you now have a tired hamster in Box B. How many hamsters do you got? Still one.
You r genius! Thanks dude! By the way do you know what is stub??
A code stub? From what I'm familiar with, it usually refers to a snippet of code. But wikipedia says otherwise.
https://en.wikipedia.org/wiki/Method_stub
ok! still quite blur but nvm! Thanks by the way
closed account (48T7M4Gy)
a stub is a function without any guts.
lol kemort sounds weird... any example??
closed account (48T7M4Gy)
It's simpler than you might think:

1
2
3
4
5
int addtwoNumbers(int a, int b)
{
     // No time, just for testing other parts, low priority, do later = no guts
    return 6;// whatever, just enough so it works
}
Topic archived. No new replies allowed.