Passing values from function to function

This is a discussion on Passing values from function to function within the C++ Programming forums, part of the General Programming Boards category; i am trying to get the hang of passing return values from function to function. in the following code: //--------------------------------------------------------------------------- ...

  1. #1
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    Passing values from function to function

    i am trying to get the hang of passing return values from function to function.
    in the following code:

    //---------------------------------------------------------------------------

    #include <vcl.h>
    #include <iostream.h>
    #pragma hdrstop

    //---------------------------------------------------------------------------

    #pragma argsused
    int main(int argc, char* argv[])
    {
    int func(int);
    int funcb(int b, int a);
    int a;
    int b;

    //send this input to other functions
    cout << "Enter a number: ";
    cin >>a;
    cout << "Enter another number: ";
    cin >> b;

    //get this back from func
    cout << "func = " <<func(a)<<"\n": //value from func
    cout << "a = " <<a<<"\n"; //what is a right here

    //mess value returned from func
    cout << "Multiply a*func(a)= " <<a*func(a)<<"\n":

    //get this back from funcb
    cout<< "funcb returned: " << funcb(a,b);

    getchar(); //stop window from closing
    return 0;
    }

    int func (int a)
    {
    return a/2; //so i can tell something happened
    }

    int funcb (int b, int a) //how do i pass int(a) returned from
    // func to funcb
    {
    return a*b; //so i can tell something happened
    }
    //---------------------------------------------------------------------------


    The original value for a -- cin >> a; -- is passed to funcb.
    I expected the value returned from func to change the value of a in main and pass the altered value to funcb.
    Does this need to be a pointer or the like.
    The header file pragma... stuff and int main(int....) stuff is automatically included in Borlands new console stuff - and is a bit over my head i might add.

    thanks ITLD

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    61
    The reason that the original value of "a" is passed to funcb() is because your not actually changin "a".
    When you say

    cout << "Multiply a*func(a)= " <<a*func(a)<<"\n":

    all that it does is print out the value of (a*func(a)).
    To have it change the value of "a" you'd say something like this

    a = a*func(a);
    cout << "Multiply a*func(a)= " <<a<<"\n":

  3. #3
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    Thanks for the response - this c++ thing is just too cool

    M.R.

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. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

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