Presentation is loading. Please wait.

Presentation is loading. Please wait.

Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.

Similar presentations


Presentation on theme: "Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be."— Presentation transcript:

1 Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be able to return more than one value –In this case, we use output parameters to pass back the additional information –The argument in this case must specify a location to put the value in, not a value

2 Function with Output Parameters 4 When we want to specify the location to store (e.g.) an integer, we must declare a pointer to an integer void separate(double num, char *signp, int *wholep, double *fracp) { double magnitude; if (num < 0) *signp = ‘-’; else if (num == 0) *signp = ‘0’; else *signp = ‘+’; magnitude = fabs(num); *wholep = floor(magnitude); *fracp = magnitude - *wholep; }

3 Function with Output Parameters –Now, if we want to call this function, we have to supply a value for the first argument and variables for the second, third, and fourth int main(void) { double value; char sn; int whl; double fr; printf(“Enter a value to analyze> “); scanf(“%lf”, &value); separate(value, &sn, &whl, &fr);

4 Function with Output Parameters 4 Notice how we specify the address of a variable - with the & operator (as in scanf) 4 In the called function, we must use *var in expressions - otherwise we will be calculating with the address of the variable, not the value!  A declaration such as int *var declares var as a pointer to an integer variable (which is declared somewhere else)

5 Function with Output Parameters 4 Note that we can pass a number (e.g. 5.24) as the first argument to the function, but we can’t pass numbers (or characters) for the other arguments since they expect addresses not values 4 What happens if we omit the & operator when calling the function?

6 Meaning of the * Symbol 4 The * symbol has three separate meanings in C –The simple one is the binary multiplication operator: var1 * var2 –In a declaration it means that the variable is a pointer to an element of the given type: char *signp –In the body of a function (e.g. in an expression), it means follow the pointer: *signp = ‘-’; myvar = *ptrvar + 1;

7 Arguments Used for Both I/O 4 We have seen arguments used for input or for output 4 We can also use a single argument for both input (pass information to the called function) and output (return information to the calling function) –To do this we must use a pointer to a variable –Let’s write a function to add switch the values of two variables

8 Arguments Used for Both I/O void switch(int *first, int *second) { int holder; holder = *first; *first = *second; *second = holder; } int main(void) { int one = 1, two = 2; /* Initialized! */ printf(“One %d Two %d\n”, one, two); switch(&one, &two); printf(“One %d Two %d\n”, one, two); }

9 Scope of Names 4 The scope of a name refers to the region of a program where a particular meaning of a name is visible (can be referenced) –We need to understand the scope of functions, variables, and constants –For a constant - #define PI 3.14 - we can use the constant only in the file in which it is declared –Arguments are visible only in the function in which they are declared

10 Scope of Names –Local variables (variables defined in a function) are visible only in that function –Global variables (variables defined outside of and before all functions) are visible to all functions in the file –Functions are visible to all other functions in the file –Arguments and local variables can be declared with the same name as global functions or variables. In this case, they hide the globals

11 Scope of Names 4 Functions cannot be nested in C (in many other languages they can, resulting in more complicated scope rules) 4 More complications arise when we use more than one file to implement a program


Download ppt "Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be."

Similar presentations


Ads by Google