Functions - Call By Value - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Functions - Call By Value

Description:

return hypotenuse; Return Statement. int find_max(int x, ... cout 'Hypotenuse = ' Pythagorus(height, base) endl; double Pythagorus(double a, double b) ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 19
Provided by: norman81
Learn more at: http://www.csis.ysu.edu
Category:

less

Transcript and Presenter's Notes

Title: Functions - Call By Value


1
Functions - Call By Value
  • overloading
  • return values part 2

2
Functions, again!
  • are self-contained blocks of code, the inner
    workings of which are invisible to the remainder
    of the program.
  • are subprograms in C .
  • perform a specific task.
  • can act on data and return a value.
  • Every C program has at least one function
    main().


3
Functions
  • Why use functions?
  • make programs easier to write, debug and maintain
    - divide and conquer!
  • Two main types of functions
  • predefined -- found in the header files
  • user-defined -- todays topic


4
The NOT rule for functions
  • The arguments in the function prototype, the
    function call, and the function header must agree
    in number, order, and type
  • Remember this rule!

5
Function Overloading
  • Two or more distinct functions may have the same
    name.
  • The data types of the arguments in the function
    calls must match those in the prototypes and in
    the definitions.
  • The same function is given multiple definitions
    or implementations. The correct one is chosen by
    the compiler, not the programmer.


6
Function Overloading
  • The functions must differ in their parameter
    lists. The type and/or number of parameters must
    be different.

Examples
double myFunction(int, int, int) int
myFunction(double, int, int) int myFunction
(double, double) void myFunction(double)

7
Function Overloading
.
// a is used // c is used // b is used // d is
used

8
Returning Values
  • A function can receive many values
  • Only one value can be directly returned

9
Returning Values
  • The return statement
  • tells the function which value to send back to
    the calling program
  • terminates the function call and returns
    immediately to the calling program

10
Return Statement
  • Syntax
  • return expression
  • Examples
  • return c
  • return hypotenuse

11
Return Statement
  • int find_max(int x, int y)
  • int maximum
  • if (x gt y) maximum x else maximum
    y
  • return maximum


12
Passing Data
  • passing by reference may give back several
    values accomplished by using references (next
    topic) using pointers (much later)


13
Passing Data - by Value
  • passing by valueA copy of a value is passed
    from the calling function to the called function.

double Pythagorus(double a, double b) a
a a b b b double c sqrt(aa
bb) return c
double Pythagorus(double a, double
b) double c c sqrt(aa bb) return
c

14
Storing Values into Parameters
call to find_max
find_max(firstnum, secnum)
find_max(x, y)
x
y
865
9090

15
Passing Data - by Value
  • void main(void)
  • double height 4.0, base 3.0
  • double Pythagorus(double, double)
  • cout ltlt Hypotenuse ltlt Pythagorus(height,
    base)ltltendl
  • . . .
  • double Pythagorus(double a, double b)
  • double c c sqrt(aa bb)
  • return c


16
Passing Data - by Value
  • double Pythagorus(double a, double b)
  • double c
  • a
  • b
  • c sqrt(aa bb)
  • return c

back in main cout ltlt height cout ltlt base

17
Passing Data - by Value
  • void print_val(int) // function prototype
  • void main(void)
  • int w 3
  • cout ltlt"w before the function call is
    "ltltwltlt\n
  • print_val(w)
  • cout ltlt"w after the function call is
    "ltltwltlt\n
  • void print_val(int q)
  • coutltlt"Value passed to the function is
    "ltltqltltendl
  • q q 2 // doubles the value
  • coutltlt"Value at the end of the function is "ltlt q
    ltltendl

18
Passing Data - by Value
  • Output
  • w before the function call 3
  • Value passed to the function is 3
  • Value at the end of the function is 6
  • w after the function call is 3
Write a Comment
User Comments (0)
About PowerShow.com