functions in programming
functions in programming
functions in programming
By
Anam Javaid
Pointers
Double Pointers
Lecture Outline
Introduction to Functions
Defining Functions
Calling Function
Function Prototype
Void Functions
Functions
• A program may be broken up into manageable functions.
• calls max(3, 4) and assigns the result of the function to the variable
larger.
Calling a Function
• Another example of such a call is:
cout << max(3, 4);
which prints the return value of the function call max(3, 4).
• When the max function is invoked, the flow of control transfers to its
definition. Once the max function is finished, it returns control to the
caller.
Calling a Function
Activation Record
• Each time a function is invoked, the system creates an activation
record (also called an activation frame) that stores its arguments and
variables for the function and places the activation record in an area
of memory known as a call stack.
• Stack is Last in First out (LIFO).
• When a function calls another function, the caller’s activation record
is kept intact and a new activation record is created for the new
function call.
• When a function finishes its work and returns control to its caller, its
activation record is removed from the call stack.
Activation Record
More about Functions
a) Function with no return type and no arguments
More about Functions
b) Function with a return type but no arguments
More about Functions
c) Function with no return type while having arguments.
More about Functions
d) Function with both return type and arguments.
Function Prototypes
• A function prototype declares a function without having to implement
it.
• Before a function is called, its header must be declared.
• One way to ensure this is to place the definition before all function
calls.
• Another approach is to define a function prototype before the
function is called. A function prototype, also known as function
declaration, is a function header without implementation. The
implementation is given later in the program.
Function Prototype
Basics of functions
Default Arguments
Inline Functions
Function Overloading