Function with Output Parameters - PowerPoint PPT Presentation

Function with output parameters
Download
1 / 11

  • 64 Views
  • Uploaded on
  • Presentation posted in: General

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Presentation

Function with Output Parameters

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


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


Function with output parameters1

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;

    }


Function with output parameters2

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


Function with output parameters3

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)


Function with output parameters4

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?


Meaning of the symbol

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;


Arguments used for both i o

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


Arguments used for both i o1

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

}


Scope of names

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


Scope of names1

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


Scope of names2

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


  • Login