Sunteți pe pagina 1din 13

Module 4

Functions
Functions
• A function is a self-contained program segment that
carries out some specific, well-defined task.
• C functions can be classified into two categories,
namely, library functions and user-definedfunctions.
• The main distinction between these two categories
is that library functions are not required to be
written by user whereas a user-defined function has
to be developed by the user at the time of writing a
program.
Functions
• Every program must have a main function(main()) 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.
• The problem may become too large and complex and as a
result the task of debugging, testing, and maintaining
becomes difficult.
• If a program is divided into functional parts, then each part
may be independently coded and later combined into a
single unit.
Functions
• These independently coded programs are called
subprograms that are much easier to understand,
debug, and test. In C, such subprograms are
referred to as “Functions‟.
• The three elements in a function are
1. function declaration
2.function definition
3. function call
Function Declaration
• All functions in a c program must be declared,
before they are invoked.
• Function declaration consists of four parts.
1. Function type (return type).
2.Function name.
3.Parameter list.
4. Terminating semicolon.
Function Declaration
• The general format of function declaration is:
Function-type function-name (parameter list) ;
Example : int sum(int a,int b);
int – function-type
sum – function-name
(int a, int b) _ Parameters (arguments)
Function Declaration
• In function declaration:
1.The parameter list must be separated by commas.
2. The parameter names do not need to be the same in
the prototype declaration and the function definition.
3. Use of parameter names in the declaration is optional.
4.If the function has no formal parameters, the list is
written as “(void)‟.
5.The return type is optional, when the function returns
int type data.
6. The return type must be void if no value is returned.
Function Declaration
• When we place the declaration above all the functions,
the prototype is referred to as a global prototype.
• Such declarations are available for all the functions in the
program.
• When we place it in a function definition (in the local
declaration section), the prototype is called a local
prototype.
• Such declarations are primarily used by the functions
containing them.
Function Definition
• Function definition includes the following elements:
1. Function name.
2. Function type.
3. List of parameters.
4. Local variable declaration.
5.Function statements.
6.A return statement.
• These elements are grouped into two parts, namely,
function header and function body.
Function Definition
• The general format of function definition is:
function-type function-name (parameter list)
{
Local variable declaration; function header

Executable statement1;
……………………… function body
Executable statement n;
Return statement;
}
function type :- specifies the type of value (data type) that is returned by
the function. If the return type is not explicitly specified, C will assume
that it is an integer type. If the function is not returning anything then
specify the return type as void.
function name :- the function name is any valid C identifier.
List of parameters :-the parameters are called formal parameters, because
they represent the names of data items that are transferred into the
function from the calling portion of the program. They are also known as
arguments or formal arguments. The identifiers used as formal
arguments are ‟local‟ in the sense that they are not recognized outside
of the function. Hence, the names of the formal parameters need not be
the same as the names of the actual arguments in the calling portion of
the program.
function body :- the function body contains the declarations and
statements necessary for performing the required task. It should include
one or more return statements, in order to return a value to the calling
portion of the program. If a function does not return any value, omit the
return statement.
Function Call
• A function can be called by simply using the function name
followed by a list of parameters (or arguments), if any,
enclosed in parentheses and separated by commas.
• The parameters appearing in the function call are referred
to as actual parameters.
• The general format of function call is:
• If the function returns a value, the function access is often
written as an assignment statement; .i.e.
variable = function name (actual parameters);
• On the other hand, if the function does not return
anything, the function call appears by itself; .i.e.
function name ( actual parameters );
Example program to find sum of two numbers
#include<stdio.h>
int sum( int, int); function declaration
void main() Actual parameters
{
int a, b, c;
printf(“enter two numbers”); main function
scanf(“%d %d”, &a, &b);
c= sum(a,b); function call
Formal parameters
printf(“sum of two numbers = %d”, c);
}
int sum ( int x, int y) function header
{
int result; Function definition
result = x + y ; function body
return(result);
}

S-ar putea să vă placă și