Function Notes

Introduction to Functions


Defining a Function


// This function rounds a double number and
// returns an integer
int round (double number)  // number passed by value
{
   int answer;

   answer = (int) (number + 0.5);

   return answer;
}

Calling a Function


Function Semantics

  1. The function call
    1. Control is transferred from the caller to callee
    2. Parameters are passed in in-order
    3. Memory is allocated for local variables (including parameters passed by value)
  2. The body of the function is executed
  3. Function Return
    1. If it is not a void function, the return value is calculated and returned to caller
    2. Memory for local variables is deallocated
    3. Control is transferred back to the caller

Passing Parameter by Value
Function Prototypes
Overloading Functions
Math functions
Character functions