Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,208 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,363 people online right now. Registration is fast and FREE... Join Now!




pass a reference

 
Reply to this topicStart new topic

pass a reference, my function does not return modified values

thompson03
8 Jul, 2008 - 01:36 PM
Post #1

New D.I.C Head
*

Joined: 12 Jun, 2008
Posts: 23

Hi, I'm having problems getting my function to return the "new" value of a variable.

The function works, I put a cout in the function and it is getting the correct value,
but the cout in main() is still the original value of the variables

I declared my function in a header file

long gcd(long a, long b, long&x, long&y) it does not seem to matter if i have long &x, long& x or long&x

in my function file i have
long gcd(long a, long b, long&x, long&y) again the spacing doesnt seem to matter
{procedure}

in my main file
i have gcd(a,b,x,y)

and cout gcd(ab,x,y) << x << y and the x and y are not changed
even though a cout in the function file shows the function is working.

Is there another location that the "&" is needed?

The book i'm reading gives one simple example of reference, based on that, and websearches , everything looks hunky dorry, but it isnt working sad.gif

i've included the files

nope, i guess not, the website wont allow .cpp extension in attachement or something

i hesitate to copy all the code, because it is quite long and in three files. but i will if asked
User is offlineProfile CardPM
+Quote Post

Hyper_Eye
RE: Pass A Reference
8 Jul, 2008 - 02:00 PM
Post #2

D.I.C Head
**

Joined: 13 Sep, 2007
Posts: 72



Thanked: 5 times
My Contributions
You may have to paste the code because it doesn't seem like your doing anything wrong. Here is a simple example I just wrote that works:

CODE
#include <iostream>

using namespace std;

void increment(int &a)
{
  a++;
}

int main()
{
  int num = 0;

  increment(num);
  cout << num << endl;

  return 0;
}

User is offlineProfile CardPM
+Quote Post

gabehabe
RE: Pass A Reference
8 Jul, 2008 - 02:01 PM
Post #3

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,556



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
If it's that big, you might not fit it all into one post...

Try adding them to a zip, and uploading the zip smile.gif
User is offlineProfile CardPM
+Quote Post

thompson03
RE: Pass A Reference
8 Jul, 2008 - 02:18 PM
Post #4

New D.I.C Head
*

Joined: 12 Jun, 2008
Posts: 23

I am compiling using the cl program that runs in the command line for visual basic (got sick of creating a new project for every snippet of code) Running windows vista home basic.

The program is an extended exercise from C++ for Mathematicians by Scheinerman

the header and function code were given in the book, I wrote the main() and the extra cout in the function

Here is header file
CODE

#ifndef GCD_H
#define GCD_H

/**
*Calculate the greates common divisor of two integers.
*Note: gcd (0,0) will return 0 and print an error message.
*@param a the first integer
*@param b the second integer
*@return the greatest common divisor of a and b
*/

long gcd(long a, long b);
long gcd(long a, long b, long&x, long&y);

#endif


the function file

CODE

#include <iostream>
using namespace std;

long gcd(long a, long b, long&x, long&y)
{
    long d; // place to hold final gcd

    // in case b=0, we have d=|a|, x =1 or -1, y arbitrary (say, 0)
    if (b==0)
    {
        if (a<0)
        {
            d = -a;
            x = -1;
            y = 0;
        }
        else
        {
            d = a;
            x = 1;
            y = 0;
        }
        return d;
    }

    // if b is negative, here is a workaround

    if (b<0)
    {
        d = gcd (a,-b,x,y);
        y = -y;
        return d;
    }

    // if a is negative, here is a workaround

    if (a<0)
    {
        d = gcd (-a,b,x,y);
        x = -x;
        return d;
    }

    // set up recursion

    long aa = b;
    long bb = a%b;
    long qq = a/b;
    long xx,yy;

    d = gcd(aa,bb,xx,yy);
    x = yy;
    y = xx - qq*yy;
    cout << x <<","<<y<<","<<xx<<","<<yy<<"\n"; // this verifies x and y are correct
    return d;
}


and the main file

CODE

#include "gcd.h"
#include <iostream>
using namespace std;

/**
*A program to find d = ax + by given a, b
*Linear Diophantine Equations
*/


int main ()
{
    long a,b,x,y;

    cout << "Enter the first number --> ";
        cin >> a;
        cout <<  endl;

    cout  << "Enter the second number --> ";
        cin >> b;
        cout << endl;

        x=1;
        y=1;

    cout << "The gcd of " << a << " and " << b <<  " is "
        << gcd(a,b,x,y) << "=" << a << "x" << x
        << "+" << b << "x" << y << endl;
    return 0;
}


This post has been edited by thompson03: 8 Jul, 2008 - 02:21 PM
User is offlineProfile CardPM
+Quote Post

Hyper_Eye
RE: Pass A Reference
8 Jul, 2008 - 02:46 PM
Post #5

D.I.C Head
**

Joined: 13 Sep, 2007
Posts: 72



Thanked: 5 times
My Contributions
I believe you are experiencing a scope problem. Try it this way:

CODE
    long gcd_val;

    gcd_val = gcd(a,b,x,y);

    cout << "The gcd of " << a << " and " << b <<  " is "
        << gcd_val << "=" << a << "x" << x
        << "+" << b << "x" << y << endl;

User is offlineProfile CardPM
+Quote Post

thompson03
RE: Pass A Reference
8 Jul, 2008 - 03:06 PM
Post #6

New D.I.C Head
*

Joined: 12 Jun, 2008
Posts: 23

Ok!
that fixed it smile.gif


now can you tell my, " why did that work ???"

since the value was ok anyway, and x and y where the problem, how in the heck did that fix x and y!!




User is offlineProfile CardPM
+Quote Post

Hyper_Eye
RE: Pass A Reference
8 Jul, 2008 - 03:25 PM
Post #7

D.I.C Head
**

Joined: 13 Sep, 2007
Posts: 72



Thanked: 5 times
My Contributions
QUOTE(thompson03 @ 8 Jul, 2008 - 06:06 PM) *

Ok!
that fixed it smile.gif


now can you tell my, " why did that work ???"

since the value was ok anyway, and x and y where the problem, how in the heck did that fix x and y!!


Your values were getting set. They just weren't set when you printed them because of the way cout works. cout actually works from right to left as is evident by <<. Try this code and you will understand:

CODE
#include <iostream>

using namespace std;

int increment(int &num)
{
  num++;

  return 0;
}

int main()
{
  int a = 0;
  int b = 0;
  int c = 0;

  a++;
  cout << a << endl;

  cout << increment(b) << b << endl;

  cout << c << increment(c) << endl;

  return 0;
}


Keep in mind that the return from the function will be first on the second print and it will be second on the third one. You would expect to see:

1
01
00

You will actually see:

1
00
10

This post has been edited by Hyper_Eye: 8 Jul, 2008 - 03:28 PM
User is offlineProfile CardPM
+Quote Post

thompson03
RE: Pass A Reference
8 Jul, 2008 - 03:40 PM
Post #8

New D.I.C Head
*

Joined: 12 Jun, 2008
Posts: 23

Ok, I get it.

Thanks.



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 12:44PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month