05 Function in C++
05 Function in C++
• A function is named
• A function is independent.
2. User-Defined Function
• Functions which are defined by user to reduce the complexity of big programs
Why User-Defined Functions
1. Every program must have a main function to indicate where the program has to begin
its execution. While it is possible to code any program utilizing only main function. It
leads to a number of problems.
2. The program may become too large and complex and as a result the task of
debugging, testing, and maintaining becomes difficult.
3. If a program is divided into functional parts, then each part may be independently
coded and later combined into a single unit. These subprograms called ‘functions’ are
much easier to understand, debug and test.
4. The length of a source program can be reduced by using functions at appropriate
places. This factor is particularly critical with microcomputers where memory space is
limited.
5. It is easy to locate and isolate a faulty function for further investigations.
6. A function may be used by many other programs.
User-defined Functions
2. Function Definition
}
Function Invocation / Function Calling
• Declared Functions are not executed unless they are called.
• Function Calling Syntax
multi(2.5, 3.8);
Function Declaration
• It must be added to program just before the main function, if we call
that function before defining it
• It tells the compiler what type of value the function returns, numbers
and types of parameters, and order in which these parameters are
expected.
Example:
float multi (float x, float y);
Difference Between Function Prototyping
and Function Definition
S.No. Function prototype Function definition
1. It declares the function. It defines the function