Thread: parameter/variable passing && void func(); (please help me...PLLLEEEAAASSSEEE!)

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    Question parameter/variable passing && void func(); (please help me...PLLLEEEAAASSSEEE!)

    Ok here is my question. How do you make subroutine things, and how do you pass the variables so you can use them in different subroutines? Ok, the following is an example that illustrates using subroutines/functions, and it is in need of variables being passed/parameters. Even though I could easily do it without the subroutines, I did it like this so I get how to do it. I made it so things need to be passed from subroutine to subroutine, main to subroutine, and subroutine to main. Could someone please copy this code and make all necessary changes (like variable passing and correct use of subroutines/functions) to make it work? Thanks, this would help me a lot and I could finally fix my awesome program.

    void func1(); //Do I need to do this? Prototype?
    void func2(); //Same thing...?
    void func3();
    void func4();

    int main()
    {
    int aa, ab, ac;
    func1();
    p=99;
    func2();
    func3();
    cout<<x<<y<<z;
    cout<<a<<b<<c;
    int gx=sum2*5
    func4();
    cout<<gh;
    cout<<cd;
    cout<<ab;
    return 0;
    }

    //FUNCTION DEFINITIONS

    void func1() //Function definition? Must I do this?
    {
    int x, y, z, p;
    x=56;
    y=34;
    z=2;
    }

    void func2()
    {
    int gh=p+x+z;
    aa=45;
    int sum=x+y+z;
    char a='hello user' // <-- Is that the right type of quotes?
    int b=99;
    int c=10;
    sum1=b+c;
    }

    void func3()
    {
    ab=55;
    cout<<sum<<a<<x;
    int sum2=sum+sum1;
    }

    void func4()
    {
    ac=ab+aa;
    cout<<gx<<a<<x<<sum1;
    int cd=70;
    gh=gh*2;
    }
    Last edited by Leeman_s; 11-03-2001 at 09:25 AM.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    The functions you've created will work but won't really do anything. They don't return anything (void) and the argument list is empty (func1( ) ).

    First:
    Let's change void func1( ) to int func1( int nValue ). And let's say func1 returns double the value of any value it is passed.

    Second:
    Let's change void func2( ) to void func2( int * pValue ). Where func2 will double the value of the integer pointed to by pValue.
    Code:
    int func1( int nValue ); // declaration
    void func2( int * pValue ); //declarartion
    
    int main( )
    {
    
      int n = 10;
    
      int nNew = func1( n ); //nNew will equl 20 and n will stay at 10
      int *pNew = &nNew;  // assign nNew to pNew
      func2( pNew ); //when this returns nNew will be 40
    
      return 0;
    
    }
    
    int func1( int nValue ) //definition
    {
       return nValue * 2;
    }
    
    void func2( int * pValue ) //definition
    {
       *pValue *= 2; //* dereference pValue and double what it points to.
    }
    And the question about char a='hello user' // <-- Is that the right type of quotes? won't work.

    Try char a[] = "hello user"; // this will create an array of 11 chars 10 for the text and 1 to null terminate the string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Virtual Inheritance: Building Menu items
    By csonx_p in forum C++ Programming
    Replies: 12
    Last Post: 01-22-2009, 12:28 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM