5 Replies - 6761 Views - Last Post: 16 October 2012 - 05:57 PM Rate Topic: -----

#1 dreadlord   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-September 12

Call by value and call by reference

Posted 16 October 2012 - 05:53 AM

Hello there,

I was learning about the call by value and by reference. I understood that call by value and call by reference clearly.
Its about calling by value and then the address.

One thing I did not understand is that, what are we doing this? For what?

See, call by value,
we make a photocopy of the formal arguments and swaps the number and the main values doesn't change

in call by reference,
we swap using address of the two numbers and here the value changes? how?

I understood the concept but I don't Understand why.
Explain

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Call by value and call by reference

#2 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Call by value and call by reference

Posted 16 October 2012 - 05:59 AM

When you pass by reference you are passing a pointer to a value in memory. So essentially both places are pointing to the same value in memory. Therefore when another function changes this value in memory, because they are both pointing to the exact same place, the change can be seen in both places.

Hope that makes sense.
Was This Post Helpful? 1
  • +
  • -

#3 Mrk   User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 66
  • Joined: 03-December 09

Re: Call by value and call by reference

Posted 16 October 2012 - 08:18 AM

I'm not sure what you mean by "why", but I will explain a few things about passing arguments to functions and why we do it. First, let's start with a situation where we don't need to pass anything:

#include<iostream>
using namespace std;

void foo();
int x = 0;    

int main()
{
   foo();
   return 0;
}

void foo()
{
   ++x;
   cout << x;
}


Here we have the function foo() that doesn't need to have x passed to it, foo can reach and manipulate x with no problem. This is because x is defined in global space, that is, outside of any function. Variables in global space are reachable by any function anytime and anywhere in the program. It makes life simple doesn't it, as any function can just reach right up and mess with the globally defined data. In a sense, this is the natural way things are, all data is naturally out in the open, available to any other code statements in the program. In the program above, x is actually incremented by one by foo, and the value stored in x is now equal to 1. This is simple and convenient for the programmer, however, it is not a good idea to have all of your data sitting out in the open accessible to any prowling functions that may be lurking in the area. That is to say, when you have a large real world program that has hundreds or thousands of functions, and teams of programmers working on the program, the last thing that you need is your data laying out in the open to be accidentally altered. Also, consider the situation when you have a globally defined variable like x which is of type int, and you have 132 functions in your program and 29 of them use that variable x. Now you decide that x needs to be changed to type float to accommodate new data, so now you have to seek out and locate all of those functions that use x and that expect x to be of type int so you can change those functions to use the new float type. That alone can be difficult to find all functions that use x and thus the program is susceptible to more bugs. However, if we are simply passing a copy (pass by value) of x to the functions that use it, then when we change x to a float all the functions are passed a copy of that new float value. So global variables are considered bad bad practice. In small programs for learning these things are not an issue, but they are issues in real world programming.

So, since using global variables is a bad idea, and since data is naturally global, compilers were designed to make data "hard to get". When we define a variable within the curly braces of a function (or within any curly braces) the compiler causes that variable to only be accessible to other code within those same curly braces. In the program above, if we were to move x inside the main function then foo would not be able to "see" x or even know that it exists. Of course, we would need to pass a copy of x to foo, or if foo needed to actually change x then we would pass the address of x to foo by way of a reference (or else a pointer) so foo could actually manipulate x directly. Since we have to deliberately work to get our functions connected to our variables the chance that our variables will be unintentionally altered is minimized.

#include<iostream>
using namespace std;

int x = 0; //Global variable - bad bad!

int main()
{
   int y = 0; // local variable - can be seen only in main.

   {
    int z = 0;
    cout << z;  // no problem
   }
  
   cout << z; // ERROR - z undefined identifier.

   return 0;
}

This post has been edited by Mrk: 16 October 2012 - 08:22 AM

Was This Post Helpful? 1
  • +
  • -

#4 Mrk   User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 66
  • Joined: 03-December 09

Re: Call by value and call by reference

Posted 16 October 2012 - 09:07 AM

<< See, call by value,
we make a photocopy of the formal arguments and swaps the number and the main values doesn't change

in call by reference,
we swap using address of the two numbers and here the value changes? how? >>

If there was a killer that wanted to kill this girl named Jane, and I passed by value a picture of Jane to the killer, so what? The killer could shoot or stab the picture all he wanted, it would do Jane no harm. However, if I passed Jane's address to the
killer ........

This post has been edited by Mrk: 16 October 2012 - 09:07 AM

Was This Post Helpful? 1
  • +
  • -

#5 AKMafia001   User is offline

  • </code.in.dream>
  • member icon

Reputation: 238
  • View blog
  • Posts: 738
  • Joined: 11-June 11

Re: Call by value and call by reference

Posted 16 October 2012 - 12:31 PM

In these tutorials some topics from chapter 6 and others from chapter 7 might be of your interest....
Was This Post Helpful? 0
  • +
  • -

#6 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Call by value and call by reference

Posted 16 October 2012 - 05:57 PM

Quote

When you pass by reference you are passing a pointer to a value in memory.

When you pass by reference you are passing the variable itself(very fine line between this and a pointer). You can consider a reference as an alias to a variable; however, it's overall implementation will vary from compiler to compiler.

Important thing to remember is that passing by pointer != passing by reference

Pointer & reference gotchyas
#include <iostream>

//Doesn't work by passing pointer by value
void badSetToNull(int *x) {
   x = 0;
}

//Works by passing double pointer by value, but not syntactically pleasing
void okSetToNull(int **x) {
   *x = 0;
}

//Works by passing reference to a pointer
void goodSetToNull(int *&x) {
   x = 0;
}

int main() {
   int *y, *x;
   x = y  = new int(10);

   std::cout<<"x address = "<<x<<std::endl; // x != 0

   badSetToNull(x);
   std::cout<<"x address = "<<x<<std::endl; // x != 0

   goodSetToNull(x);
   std::cout<<"x address = "<<x<<std::endl; // x = 0

   x = y;
   std::cout<<"x address = "<<x<<std::endl; // x != 0

   okSetToNull(&x);
   std::cout<<"x address = "<<x<<std::endl; // x = 0

   delete y;

   std::cin.ignore();
   std::cin.get();
   return 0;
}


This post has been edited by jjl: 17 October 2012 - 02:25 PM

Was This Post Helpful? 3
  • +
  • -

Page 1 of 1