Results 1 to 18 of 18

Thread: Substitution of variables in functions?

  1. #1

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    Substitution of variables in functions?

    Does cube(side) send the value of side to s at the top?

    inline float cube(const float s) {return s*s*s;}
    int main() {

    float side; cin >> side;
    cout << "Volume of cube with side" << side<< "is "
    << cube(side) << endl;

    return 0;
    }

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yes. But only the value. If you change s in cube then side won't change because s is a copy of side.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    But cube s will return the value of side*side*side, right.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Logically yes. Actually it will return the value of s*s*s, but since s has the same value as side the result is effectivly side*side*side.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Is the way that is set up uncommon? Is there a better way to do it?

  6. #6
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    That looks normal to me. Unless you want to pass by reference:

    Code:
    inline void cube(float& s) {s = s * s * s;} 
    int main() { 
    
    float side; cin >> side; 
    float se = side;
    cube(se);
    
    
    cout << "Volume of cube with side" << side<< "is " 
    << se << endl; 
    
    return 0; 
    }
    but that is totally bizzare in this example
    VS.NET 2003

    Need to email me?

  7. #7

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    Could you explain how pass by reference works there. I never saw much use in pass by reference but you did something a little different.

  8. #8
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Passing by reference means a function can change the value of the variable. Here, it doesn't receive just a value, by an entire variable which it can write to.
    Try this:
    Code:
    void F(int x)
    {
    	cout << "F: " << x << endl;
    	cout << "F: Changing to 17..." << endl;
    	x = 17;
    }
    
    void G(int& x)
    {
    	cout << "G: " << x << endl;
    	cout << "G: Changing to 42..." << endl;
    	x = 42;
    }
    
    void H(int& x)
    {
    	cout << "H: Changing to 5..." << endl;
    	x = 5;
    }
    
    int main()
    {
    	int a, b, c;
    	
    	cout << endl;
    	// Test F
    	a = 1;
    	cout << "main: " << a << endl;
    	F(a);
    	cout << "main: " << a << endl;
    	
    	cout << endl;
    	// Test G
    	b = 2;
    	cout << "main: " << b << endl;
    	G(b);
    	cout << "main: " << b << endl;
    	
    	cout << endl;
    	// Test H
    	// Note that the variable c is NOT INITIALIZED
    	H(c);
    	cout << "main: " << c << endl;
    	
    	return 0;
    }

  9. #9
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    By the way, references are new to C++.
    In C you have only pointers. In C++ you have both.
    For an example of how pointers are written, the above function G in C:
    Code:
    void G(int *x)
    {
    	printf("G: %d\n", *x);
    	printf("G: Changing to 42...\n");
    	*x = 42;
    }
    And the call from main:
    Code:
    	b = 2;
    	printf("main: %d\n", b);
    	G(&b);
    	printf("main: %d\n", b);
    References have a simpler syntax, so use them if you can.

  10. #10
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    References have a disadvantage though. They can not be changed.

    e.g.

    Code:
    int z[20];
    int* p;
    int& e = z[1];
    p = &z[1];
    
    p = &z[2];
    e = z[8]; //will not work!
    VS.NET 2003

    Need to email me?

  11. #11
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Or an advantage, depending on how you look at it.
    (If you look at it in a way that makes it seem like an advantage, you're looking at OOP.)

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can change a variable *through* any non-const reference to it. You cannot change the reference *itself* though once set. This is useful because, you avoid the following check:
    Code:
    void func(large_struct *ptr) {
        if(!ptr) return;
    
        /* ... */
    }
    
    void func(large_struct &ref) {
        // ...
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    In which you can also, of course, unconventionally cheat:
    Code:
    func(*static_cast<large_struct*>(0)); // :rolleyes:

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Indeed. But in doing that, all bets are off, and most compilers will pick up on the attempt to dereference 0 anyway.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Originally posted by Yonatan
    In which you can also, of course, unconventionally cheat:
    Code:
    func(*static_cast<large_struct*>(0)); // :rolleyes:
    That's still the problem of pointers, not references.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    If I ever get Dev C++ to compile anything I will learn more and be able to figure these things out.

  17. #17
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Please post the errors you get, we may be able to help you out than.. are you sure all the (include) paths are set correctly? (just a guess...)
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  18. #18

    Thread Starter
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037
    I posed a new thread for it. Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width