You are on page 1of 11

Functions

User-Defined Function, Function Definition


Function
• A function is a named, independent section of C code that performs a
specific task and optionally returns a value to the calling program.

• A function is named

• A function is independent.

• A function performs a specific task.

• A function can return a value to the calling program.


Types of Functions
1. Pre-Defined functions/Built-in functions/Library functions
• Functions which are already defined and comes with compiler package we
can directly use these function
• Printf(), Scanf()

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

1. Function Declaration (Function Prototype)

2. Function Definition

3. Function Invocation (Function Call)


Function Definition
A function definition, also known as function implementation shall
include the following elements.
1. Function Name
2. Function Type
3. List of Parameters
4. Function Local Variable
5. Function Statements
6. A Return statement
Above elements are grouped into two parts, namely,
7. Function header (First three Elements)
8. Function Body (Second three Elements)
User-defined Function SYNTAX and
Example
function_type function_name(parameter_list)
{ EXAMPLE
local variable declaration;
executable statement_1; float multi (float x, float y)
executable statement_2; {
----------------------------
float result;
----------------------------
return statement;
result = x * y;
}
return (result);

}
Function Invocation / Function Calling
• Declared Functions are not executed unless they are called.
• Function Calling Syntax

function_Name (Actual Parameters);

• Function Calling Example;

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.

• There is no need for a prototype if the function is called after its


definition
Function Declaration Syntax
Like variables, all functions in program must be declared, before they
are invoked, A function declaration (also known as function prototype)
consists of four parts.
The general format is:
Function_type function_name(parameter_list);

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

2. It ends with a semicolon. It doesn’t end with a semicolon.

3. Declaration need not include Definition should include names for


parameters. the parameters.

You might also like