quiestion on function parameters

This is a discussion on quiestion on function parameters within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm learning about the different ways of passing arguments for functions and everything (i think!) is clear in ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    55

    quiestion on function parameters

    Hi all,

    I'm learning about the different ways of passing arguments for functions and everything (i think!) is clear in Koeings book (esp. p58). However, when I'm trying to pass an argument to modify it and keep its new value outside the function then it runs like I'm passing references. Can you please explain why the program below passes "final" as a refence even though I have put "bouble& fin"

    Thank you
    Spiros


    Code:
    #include<iomanip>
    #include<ios>
    #include<iostream>
    #include<string>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::setprecision;
    using std::string;
    using std::streamsize;
    
    double grade(double mid, double& fin, double sum_count) {
            fin+=100;
            cout << endl
                   << "Final grade (inside function) is: " << fin << endl;
            return 0.2*mid + 0.4*(fin-100) +0.4*sum_count;
    }
    int main()
    {
            cout << "Enter your midterm and final exam grades: ";
            double midterm, final;
            cin >> midterm >> final;
            cout << "Enter your homework grades, followed by an CTRL-D to stop: ";
    
            int count=0; double sum=0; double x;
            while (cin >> x) {
                    ++count;
                    sum += x;
            }
    
            streamsize prec = cout.precision();
            cout << "Your total grade is " << setprecision(3)
                    << grade(midterm, final, sum/count) << endl
                    << "Also final grade (outside function) is: " << final
                    << setprecision(prec) << endl;
            return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    double& fin is a reference.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I had to reread this to understand the question (my dyslexia kicking in on me...). You use the & operator to denote a reference. So you are calling final by reference.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    it isn't. you call functions, not variables.

    It's being passed by reference because you have a reference as the parameter that you are passing to in your function.

    double grade(double mid, double& fin, double sum_count)

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    55
    thanks for the replies but I'm still getting confused. I'm reading Koeing's book on p58 again and again, but I still get the impression that if you put & to a function parameter then you work on the parameter itself rather than a copy...

    Koeing example on
    Code:
    const vector<double>&
    and he says:
    In this type, the & asks the implementation not to copy the argument, and the const promises the program wil not change the parameter
    doesn't that mean hat by putting & we are not using a reference?

    I've tried my program without the & but the results are the same; "final" does not change outside the function.

    thanks

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok, what he means by copy and what you think he means by copy seem to vary. He means copy to the stack. You mean copy as in faximaly(sp)?

    Think of double& x as saying I don't want to create my own double and call it x (copy), I want the exact one you are handing to me (reference).

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    doesn't that mean hat by putting & we are not using a reference?
    On the contrary, the & after the type name means that pass by reference is used.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2004
    Posts
    55
    thank you for your time...

    Things are improving now and I understand what we mean when saying "reference". But I'm still confused

    Koeing on p54 sgives the example:
    Code:
    vector<double> homework;
    vector<double>& hw = homework;    // hw is a synonym for homework
    and states:
    we are saying that hw is another name for homework. From that point on, anything we do to hw is equivalent to doing th same thing to homework, and vice versa.
    Doesn't that mean that if we eg increment an element in "hw" by 50, then we do the same thing to the element in "homework"?

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Using my definition again think of it as saying, hw wants your homework. Not hw wants its own homework.

    It wants consider itself the same as homework, not make a new homework and copy all its contents to itself.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Doesn't that mean that if we eg increment an element in "hw" by 50, then we do the same thing to the element in "homework"?
    Yes. Of course, try it out yourself to convince yourself that it happens.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Why don't you try things?
    Code:
    #include <iostream>
    int main()
    {
        int n = 50;
        int& r = n;
        r += 10;
        std::cout << n << ' ' << r << '\n';
    }
    Try with and without the & to see the difference.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User
    Join Date
    Nov 2004
    Posts
    55
    I have tried it on mine but without success, whereas anon's example works exactly as I expected to. However, when I put & and take it off in my program, then it doesn't make any difference in "final" so I suppose there's another mistake in my program... Any ideas why this happens in my program?

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Could you give input/output for an example run?

    Code:
            cout << "Your total grade is " << setprecision(3)
                    << grade(midterm, final, sum/count) << endl
                    << "Also final grade (outside function) is: " << final
                    << setprecision(prec) << endl;
    Within this statement, the value of final is used twice and it is changed in the meantime (grade has side-effects). I'm not sure if this is undefined like i = i++; but to be safe you might break it up into two statements:
    Code:
            cout << "Your total grade is " << setprecision(3)
                    << grade(midterm, final, sum/count) << endl;
            cout  << "Also final grade (outside function) is: " << final
                    << setprecision(prec) << endl;
    Anyway, for me it gives the expected output (fin == final) either way.
    Last edited by anon; 04-26-2008 at 02:40 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    Registered User
    Join Date
    Nov 2004
    Posts
    55
    it works!
    I've split the cout statement into two as you suggested and it worked. Thanks so much, it would've taken me donkeys years to figure this out on my own...

    cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 03:35 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21