Function with Output Parameters - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Function with Output Parameters

Description:

Scope of Names ... a name refers to the region of a program where a particular meaning of a name is ... declared with the same name as global functions or ... – PowerPoint PPT presentation

Number of Views:92
Avg rating:3.0/5.0
Slides: 12
Provided by: timoth73
Category:

less

Transcript and Presenter's Notes

Title: Function with Output Parameters


1
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

2
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 lt 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 analyzegt )
  • scanf(lf, value)
  • separate(value, sn, whl, fr)

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

5
Function with Output Parameters
  • Note that we can pass a number (e.g. 5.24) as the
    first argument to the function, but we cant 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?

6
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

7
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
  • Lets 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
  • 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
  • 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
Write a Comment
User Comments (0)
About PowerShow.com