Thread: Question about functions

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    100

    Question about functions

    Hi,

    I understand that any function declared after a global variable can access that variable, and that a variable declared inside a function is local and can only be used by that function.

    My first question is can that local variable declared in a function be passed as an argument to another function? And if it can, can it only be passed to a function whose definition comes after it or can it be passed to previous function definitons as well? Example: (see comments)


    Code:
     double Tax(double returned_gross_pay)
    
    {
    	double l;          /*can this be passed to the next function?*/
    	l = returned_gross_pay * 0.15;
    
    	return l;
    }
    
    
    double Net_Pay(double returned_net_pay, double returned_tax)
    
    {
    
    	double m;      /*can this be passed to the previous function?*/
    
    	m = returned_net_pay - returned_tax;
    
    
    	return m;
    
    }

    My other question is similar. With the functions themselves, I know that a function can contain another function that was defined after it. Example:


    Code:
     double Tax(double returned_gross_pay)
    
    {
    	double l,
                               a;
    
    	l = returned_gross_pay * 0.15;
    
    	a = double Net_Pay(); /*the following function*/
                  
                    return l;
    }
    
    
    double Net_Pay(double returned_net_pay, double returned_tax)
    
    {
    
    	double m;
    
    
    	m = returned_net_pay - returned_tax;
    
    
    	return m;
    
    }
    But can this also be done in reverse? Could I put Tax() inside Net_Pay() ?

  2. #2
    old man
    Join Date
    Dec 2005
    Posts
    90
    Quote Originally Posted by richdb
    My first question is can that local variable declared in a function be passed as an argument to another function?
    C passes by value, so the value of the variable is returned rather than the variable itself. Thus it doesn't matter whether the variable is in scope or not -- what you get back is a copy. Here's a simple example of pass by value:

    Code:
    /* ... */
    
    void
    open_file (FILE *fp, char *path, char *mode)
    {
      /* open the file */
    }
    
    /* ... */
    FILE *fp;  char *fn = "myfile";  char *mode = "w";
    open_file (fp, fn, mode);
    /* ... */
    This looks like it will work, and (all things being equal) the file will open with no error ... but the fp in the function is not the same variable the caller is using. After the call, the fp you want to use is not initialized -- a copy of it was.

    However, it will work if you return fp and use it to initialize the fp of the caller ...

    Code:
    /* ... */
    
    FILE *
    open_file (char *path, char *mode)
    {
      FILE *fp;
      /* open the file */
      return fp;
    }
    
    /* ... */
    FILE *fp;  char *fn = "myfile";  char *mode = "w";
    fp = open_file (fn, mode);
    /* ... */
    My other question is similar. With the functions themselves, I know that a function can contain another function that was defined after it.
    You don't need to fuss over the sequence of your functions when you can simply prototype them up top.

  3. #3
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    You don't need to fuss over the sequence of your functions when you can simply prototype them up top.
    I agree on that. Define all your functions at the top and you wont need to worry about these kind of problems and careless bugs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's question about functions.
    By Crocodile23 in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 07:00 AM
  2. Functions Question
    By audinue in forum C Programming
    Replies: 2
    Last Post: 01-09-2009, 09:39 AM
  3. functions question.
    By Boozel in forum C Programming
    Replies: 1
    Last Post: 02-23-2008, 12:38 AM
  4. Question concerning functions
    By Warrax in forum C++ Programming
    Replies: 5
    Last Post: 04-04-2007, 11:00 AM
  5. Question about creating flash functions
    By jbh in forum C++ Programming
    Replies: 8
    Last Post: 11-21-2005, 09:39 AM