Vous êtes sur la page 1sur 6

Functions:

F U N C TI O N S

A large and complex program can be divided into several functional parts. Each
part is called a sub-program. In C, such subprograms are referred to as functions.

Definition:

A function can be defined as a self-contained block of code that performs a specific


task.

Types of functions:

In C, functions can be classified into 2 types.

i. Library (or) Pre-defined functions


ii. User-Defined functions

i. Pre-Defined functions:
A function that was already defined in C software is known as a pre-defined
function.

Ex: printf(), scanf(), clrscr() etc.

ii. User-Defined functions:


A function that has to be defined by the programmer is known as an user-
defined function. main() is the best example for user-defined function.

Creation of User-Defined functions:


A function can be created by us using the following elements:

i. Function Definition

ii. Function Call / Invoke

iii. Function Prototype/ Declaration

i. Function Definition:

A function definition also known as function implementation. It includes the


following 2 parts.

i. Function Header
ii.Function Body

Function Header:
A function header consists of 3 parts i.e., function type (return type), function name
and formal parameter list.

The function type specifies the type of value that the function is expected to return. The
function name is any valid C identifier. The parameter list declares the variables that
will receive the data sent by the calling program.

The basic format of a function definition is:

<returntype> function_name(parameter_list)
{
statement(s);
}

Ex: int sum(int a,int b)


{
return(a+b);
}
ii. Function Call:

After defining a function , we must invoke the function to execute the function body. It
can be done by using the function name following by the list of actual arguments.

function_name(actual_args);

ex: sum(10,20);

iii. Function Declaration:

Like variables, all functions in C program must be declared before they are
invoked. A Function declaration (prototype) consists of 4 parts.

returntype fun_name(parameter_list);

ex: int sum(int a, int b);

CATEGORIES OF FUNCTIONS:

A function depending on whether arguments are present (or) not and


whether it has a return type or not , may belong to any of the following categories:

i. No Arguments and No Return value:


When a function has no arguments, it does not receive any data from the
calling function. Similarly, when it does not return a value, the calling function does
not receive any data from the called function.

function1() function2()

{ {

------ ------
------ ------

function2() }

-------

No data communication b/w functions

Ex: display()

printf(%d, x);

ii. Arguments and Return Values:

In this method, the calling function sends the data to the called function and also the
called function returns a value i.e., there is a two-way communication between the calling
and called functions.

function1() function2(y)

{ {

------- -------

------- return(e);

function2(x); }

-------

Ex: float read(float f)

return(f);

}
iii. Arguments and No return values:

This approach seems to be wiser because the calling function can check for
the validity of data if necessary before it is handed over to the called function.

function1() function2(y)

{ {

------ -------

------ -------

function2(x); }

------

Ex: show(float i)

printf(%f, i);

iv. No Arguments but Return values:

There could be occasions when we define a function that does not take any
argument but returns a value to the calling function.

function1() function2()

{ {

-------- --------

-------- return(e);
function2(); }

--------

Ex: int read()

int a=5;

return(a);

Vous aimerez peut-être aussi