Title: Functions - Call By Value
1Functions - Call By Value
- overloading
- return values part 2
2Functions, 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().
3Functions
- 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
4The 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!
5Function 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.
6Function 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)
7Function Overloading
.
// a is used // c is used // b is used // d is
used
8Returning Values
- A function can receive many values
- Only one value can be directly returned
9Returning 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
10Return Statement
- Syntax
- return expression
- Examples
- return c
- return hypotenuse
11Return Statement
- int find_max(int x, int y)
- int maximum
- if (x gt y) maximum x else maximum
y - return maximum
12Passing Data
- passing by reference may give back several
values accomplished by using references (next
topic) using pointers (much later)
13Passing 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
14Storing Values into Parameters
call to find_max
find_max(firstnum, secnum)
find_max(x, y)
x
y
865
9090
15Passing 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
16Passing 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
17Passing 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
18Passing 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