d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Call By Reference
Add Reply New Topic New Poll
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Oct 14 2013 12:46pm
How does it work? For me it looks like magic '-', example;

I got a function

Code

void max_min(int V[], int n, int &min, int &max)
{
//Add code
}


and when I call it like this

Code

int n = 12;
int V;
int min = 0;
int max = 0;

max_min(V,n,min,max);


Why can I rename min/max to what ever I want and it still compiles correctly? What information am I missing?

Thanks
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Oct 14 2013 01:32pm
I just ran into another problem -..-

"Implement the function read_seq that reads a senquence of n≥0 non-negative integers into an array V. Ignore any negative values entered by the user"

So bacisly, some code that ignores negative numbers and add the other into the array.. how do I still continue typing after negative numbers have been typed in? I guess I need to use string?

The function

Code

//Read a sequence of n integers and store them in V
//Negative integers should be simply ignored
void read_seq(int V[], int n = 12)
{

}
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Oct 14 2013 03:43pm
Member
Posts: 1,732
Joined: Sep 3 2007
Gold: 5,991.00
Oct 19 2013 11:12am
Quote (bomben @ Oct 14 2013 02:46pm)
How does it work? For me it looks like magic '-', example;

I got a function

Code
void max_min(int V[], int n, int &min, int &max)
{
    //Add code
}


and when I call it like this

Code
int n = 12;
int V;
int min = 0;
int max = 0;

max_min(V,n,min,max);


Why can I rename min/max to what ever I want and it still compiles correctly? What information am I missing?

Thanks



& - pretty much means that you're using a pointer to a variable, so instead of having the program go "here's the value for x, work on it", it says "here's the location where x is, do whatever you want to it"

In every function the variable names are locally declared, so if you do
Code

void swap(int& x, int& y){
int temp = x;
x = y;
y = temp;
}


you can plug any integer values and variables from any other function in there.
Member
Posts: 237
Joined: Aug 6 2011
Gold: 6,026.00
Oct 20 2013 04:14am
Quote (bomben @ Oct 14 2013 08:46pm)
Why can I rename min/max to what ever I want and it still compiles correctly? What information am I missing?


The min and max you're declaring are in the caller's scope. Their name are irrelevant to the callee's scope. Invoking a routine that takes arguments by reference is transparent on the caller side, i.e. it doesn't change how you call, only how the callee deals with the arguments. The difference is sensible however. Using classic arguments, the callee with declare the variables within its own scope, copy the data from the arguments to it, and those will expire when the callee exits its scope. When the arguments are passed by reference, the callee creates pointers and points them to the memory location of the arguments it received by reference. In this case all modifications done to the arguments will be applied to the original.

Quote
how do I still continue typing after negative numbers have been typed in? I guess I need to use string?


What do strings have to do in this? You have an array of integer, you cycle through it with a for or a while loop and only save the positive values.

This post was edited by flyinggoat on Oct 20 2013 04:15am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll