passing variables from main to another function

This is a discussion on passing variables from main to another function within the C Programming forums, part of the General Programming Boards category; hey guys i want to be able to pass an unsigned variable from the main function to a seperate function ...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    passing variables from main to another function

    hey guys i want to be able to pass an unsigned variable from the main function to a seperate function how could i do that?


    eg

    Code:
    int main(void)
    {
    
      unsigned month;
    
    }
    
    "how can i pass the unsigned month into the parameter so i can use it in this function"
    
    
    unsigned getMonth()  
    
    {
    
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well 'getMonth' from the sound of it, shouldn't be passed anything, especially considering that you're returning one already. Just do:
    Code:
    int main( void )
    {
        unsigned int month;
    
        month = getMonth( ); /* call your function, assing the return to 'month' */
    
        ... stuff ...
    
        return 0;
    }
    If you want to pass it, and have that updated, then you'll need to pass a pointer to it:
    Code:
    void getMonth( unsigned int *month )
    {
        ...fill in month...
    }
    
    int main( void )
    {
        unsigned int month;
    
        getMonth( &month );
    
        ... stuff ...
    
        return 0;
    }
    So in short:
    1 - Return it and assign the returned value.
    2 - Pass a pointer to it and modify that.

    However, if all you want to do is pass the value around, and not change it in the function, then simply pass it like any other variable:
    Code:
    void printMonth( unsigned int month )
    {
        ... print out the month you pass ...
    }
    
    int main( void )
    {
        unsigned int month;
    
        ... stuff ...
    
        printMonth( month );
    
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Quote Originally Posted by quzah
    If you want to pass it, and have that updated, then you'll need to pass a pointer to it:
    Code:
    void getMonth( unsigned int *month )
    {
        ...fill in month...
    }
    
    int main( void )
    {
        unsigned int month;
    
        getMonth( &month );
    
        ... stuff ...
    
        return 0;
    }
    I disagree -- at least from a 'purist' standpoint. If passing back a single value, IMO it should be passed back via return rather than the param list.
    Then the calling function can explicitly change the value if need be:
    Code:
    unsigned int getMonth( unsigned int month )
    {
        ...fill in month...
        return newmonth;
    }
    
    int main( void )
    {
        unsigned int month;
    
        // function does it's thing and returns the new month
        month = getMonth( month ); 
    
        ... stuff ...
    
        return 0;
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 10-15-2008, 04:38 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. passing counters between function
    By BungleSpice in forum C Programming
    Replies: 18
    Last Post: 02-21-2004, 06:16 PM

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