function with output parameters

11
Function with Output Parameters We have seen that functions can return a single value or no value (void return type) 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

Upload: hart

Post on 07-Jan-2016

28 views

Category:

Documents


0 download

DESCRIPTION

Function with Output Parameters. We have seen that functions can return a single value or no value ( void return type) 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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Function with Output Parameters

Function with Output Parameters

We have seen that functions can return a single value or no value (void return type)

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

Page 2: Function with Output Parameters

Function with Output Parameters

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;

}

Page 3: Function with Output Parameters

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);

Page 4: Function with Output Parameters

Function with Output Parameters

Notice how we specify the address of a variable - with the & operator (as in scanf)

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)

Page 5: Function with Output Parameters

Function with Output Parameters

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

What happens if we omit the & operator when calling the function?

Page 6: Function with Output Parameters

Meaning of the * Symbol

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;

Page 7: Function with Output Parameters

Arguments Used for Both I/O

We have seen arguments used for input or for output

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

Page 8: Function with Output Parameters

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);

}

Page 9: Function with Output Parameters

Scope of Names

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

Page 10: Function with Output Parameters

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

Page 11: Function with Output Parameters

Scope of Names

Functions cannot be nested in C (in many other languages they can, resulting in more complicated scope rules)

More complications arise when we use more than one file to implement a program