UNIT I – Chpt II - Functions
Modular Programming
• Modular programming: breaking a program up into smaller, manageable functions or
modules
• Function: a collection of statements to perform a task
• Motivation for modular programming:
– Improves maintainability of programs
– Simplifies the process of writing programs
User defined Functions
• In order to create a user defined function we need to have three elements in a program i.e
function prototype/declaration, function call and function definition.
• Function call: statement causes a function to execute
• Function definition: statements that make up a function
• Definition includes:
• return type: data type of the value that function returns to the part of the program
that called it
• name: name of the function. Function names follow same rules as variables
• parameter list: variables containing values passed to the function
• body: statements that perform the function’s task, enclosed in {}
• Calling a Function:
• A function call is giver inside the main block.
• To call a function, use the function name followed by () and ;
printHeading();
• When called, program executes the body of the called function
• After the function terminates, execution resumes in the calling function at point of
call.
• Function Prototypes:
• Ways to notify the compiler about a function before a call to the function:
• Use a function prototype (function declaration) – like the function definition
without the body
• Prototype: void printHeading();
• Place prototypes near top of program
• Program must include either prototype or full function definition before any call to the
function – compiler error otherwise