Thread: question about using returned values

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    8

    question about using returned values

    I have another question. I also read that the variables declared in a function can only stay in the function they are declared in because of scope rules. Does that mean that the calculations done in one function can't be used in another function? I'm really confused though, because when I wrote a function that printed the name, hours worked, and hourly rate inputted by the user in another function, it printed the name, but not the hours worked or the hourly rate (they were printed as 0.00).

    So how can I use the info obtained in the userInput() function in another function it if the variables declared in a function are restricted to only the functions they are declared in? I've also tried declaring external variables after declaring the function double calcPay (double hoursWorkedArray[], double hourlyRateArray[]) and before the { brace , but I got an error when I tried to do that.

    Code:
    void userInput(char nameArray[][30], double hoursWorkedArray[], double hourlyRateArray[])
    	{
    		int i;
    		for (i=0; i<5; i++)
    		{
    		printf("Enter name.\n");
    		scanf("&#37;s", nameArray[i]);
    		printf("How many hours did they work this week?\n");
    		scanf("%lf", &hoursWorkedArray[i]);
    		printf("What is their hourly rate?\n");
    		scanf("%lf", &hourlyRateArray[i]);
    
    		}
    		
    		
    	}
    
    // calculates base,gross,overtime, and net pay
    double calcPay (double hoursWorkedArray[], double hourlyRateArray[])
    {
    	double basePay[5], overTime[5], grossPay[5], netPay[5];
    		int i;
    		for (i=0; i<5; i++)
    		{
    			if (hoursWorkedArray[i] > 40)
    			{
    				basePay[i] = hourlyRateArray[i] * hoursWorkedArray[i];
    				overTime[i] = (hoursWorkedArray[i] - 40) * 1.5 * hourlyRateArray[i];
    				grossPay[i] = (hourlyRateArray[i]*40) + overTime[i];
    				netPay[i] = (hourlyRateArray[i]*40) + overTime[i] - ((hourlyRateArray[i]*40) + overTime[i])*.2;
    			
    			}
    			else
    			{
    				basePay[i] = hourlyRateArray[i] * hoursWorkedArray[i];
    				//taxesOwed[i] = (hourlyRateArray[i]*40) * .2;
    				grossPay[i] = (hourlyRateArray[i]*40) * .8;
    				overTime[i] = 0;
    				netPay[i] = (hourlyRateArray[i]*40) + overTime[i] - ((hourlyRateArray[i]*40) + overTime[i])*.2;
    				
    				return basePay[i], grossPay[i], overTime[i], netPay[i];
    
    			}
    		}
    	
    
    }
    Last edited by pokiepo; 07-01-2008 at 05:15 PM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Three ways. By passing a pointer to the variables you need as parameters to the function or by declaring the variables global, so the scope will work for you. Or returning the value of the variable

    EDIT: some code
    Code:
    int global;
    
    int fun(int *a, in *b) {
        int in1, in2;
        scanf("&#37;d", &in1);
        scanf("%d", &in2);
        *a = in1;
        *b = in2;
        global = in1;
        return in1;
    }
    1) You want to get two inputs. So you pass two pointers. The value of in1, in2 will be stored where the pointers point. So it will be saved.
    2) Even though in1, in2 are local and there scope will end when fun returns, you can make fun return the value of one of them. Then you could do like var = fun(a,b) and store the value at var. If you want to return two values then that's tricky. You would have to make a struct or an array and return that.
    3) global is outside the function. So you can use it inside the function to store a value.

    If you are wondering why there are local variables there are many reasons. Like efficiency.
    Last edited by C_ntua; 07-01-2008 at 05:42 PM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You must not do this.
    You must fix those scanfs.
    http://cpwiki.sourceforge.net/Common...kes_and_errors
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    8
    Quote Originally Posted by C_ntua View Post
    Three ways. By passing a pointer to the variables you need as parameters to the function or by declaring the variables global, so the scope will work for you. Or returning the value of the variable

    EDIT: some code
    Code:
    int global;
    
    int fun(int *a, in *b) {
        int in1, in2;
        scanf("%d", &in1);
        scanf("%d", &in2);
        *a = in1;
        *b = in2;
        global = in1;
        return in1;
    }
    1) You want to get two inputs. So you pass two pointers. The value of in1, in2 will be stored where the pointers point. So it will be saved.
    2) Even though in1, in2 are local and there scope will end when fun returns, you can make fun return the value of one of them. Then you could do like var = fun(a,b) and store the value at var. If you want to return two values then that's tricky. You would have to make a struct or an array and return that.
    3) global is outside the function. So you can use it inside the function to store a value.

    If you are wondering why there are local variables there are many reasons. Like efficiency.
    Thanks! That was very helpful ^^

    I have some more questions though:
    1) How come you wrote

    *a = in1;
    *b = in2;

    instead of

    *a = &in1;
    *b = &in2;

    ?

    2) After you have passed the pointer the the variables, would you still need to write "*a" if you want to use the value stored at *a in another function? Or would you just need to write "a"?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by pokiepo View Post
    I have some more questions though:
    1) How come you wrote

    *a = in1;
    *b = in2;

    instead of

    *a = &in1;
    *b = &in2;

    ?
    The later makes no sense.
    You are trying to assign the address of something to the value where the pointer points.
    As a rule, pointers are supposed to contain addresses, not some other type of variable.
    The first example is correct because it changes the value where the pointers point to with sensible data.

    2) After you have passed the pointer the the variables, would you still need to write "*a" if you want to use the value stored at *a in another function? Or would you just need to write "a"?
    Yes, because it's a pointer.
    Unless, you pass its value to a function. But that's pass-by-value, thus you cannot modify the original data.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Think of it this way.
    Code:
    int i, *p, **pp;
    At this point i is of type int. So is *p, and so is **p. Thus, since these are of the same type, you can easily assign values between these expressions and compare them and so on.

    &i, p, and *p are of type int*.
    &p and p are of type int**. (You can't get an int** from i by itself.)

    Hopefully that helps.

    Of course, that's not the only thing you have to keep in mind. If you pass an ordinary variable to a function, changes made to it will not be reflected in the calling function. You have to have at least one level of indirection for such changes to be visible.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You should read more about pointers. You typically do two things with pointers.
    1) You make the pointer point at a variable. You do this:
    Code:
    pointer = &variable
    The types must match. If variable is a float then pointer is a *float. The * when declaring a variable means a pointer to that type.
    2) You want to use the value of the variable being pointed by the pointer. You do this:
    Code:
    int *ptr;
    int a, b, sum;
    ptr = &a; //the ptr points at variable a
    sum = a + b; //you get the sum
    sum = *ptr + b; //you get the same sum
    *ptr == a; //this is TRUE

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Note that you'd probably want to initialize a and b so that you don't use undefined values, but still, it's a good example.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  2. Question about assigning values
    By DCMann2 in forum C Programming
    Replies: 7
    Last Post: 04-18-2008, 07:36 AM
  3. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 05:01 PM
  4. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 03:52 PM
  5. Question about ignoring values
    By jaisch in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2005, 12:06 AM