CHAP 11 (Functions)
CHAP 11 (Functions)
FUNCTIONS
Definition of functions:
A function is a block of instructions that together perform a specific task. Every C program has at
least one function, main ( ).you can also write your own functions and use them just as you use
functions in a C library.
hen your program starts, main ( ) is called automatically and it is always executed first, main ( )
might called other functions, some of which might called still others.
Built in functions:
Many of the operations, like taking the square root of a number, sine value of a number
etc. will be frequently used by many programmers in their programs. Such operations are
Programmed and stored in Turbo C++ library, so that they can be called through any program in
the form of functions. These functions are called library functions or built-in functions.
1. Function prototype:
The declaration of a function is called its prototype. It is a line written before the main ( ) function.
The prototype tells the compiler in advance about some characteristics of a function used in the
program.
The general structure of the function protype is :
Type function _ name ( type argument-1, type argument-2,..................... );
It has three main components. These are:
1. Data type of the function
2. Name of the function
3. Arguments
Argument:
The argument come inside the parentheses , preceded by their type and separated by commas. If
the function does not use any argument, the word void is used inside the parenthesis.
2. Function definition:
The function definition is the function itself. The general structure of the function definition is :
Type function _ name (type argument – 1, type argument – 2 , . . .)
{
Body of function;
}
The function definition has two parts:
1. Function header
2. Body of function
Function header:
The function begins with a header which is exactly same as the function prototype except it must
not be terminated with semicolon (;). Header specifies the name of the function. It also describes
whether you are using a simple function or passing the arguments or returning the value from the
function.
Body of function:
It is the main body of the function. Al statements are written in this part of the program. The
statements in the body of the function are written to perform a specific task.
3. Function calling:
A user defined function is called from the main program simply by using its name, including the
parentheses which follow the name. The parentheses are necessary so the compiler knows you are
referring to a function and not a variable that you forgot to declare.
Function With Or Without Arguments:
It would not be useful if all functions were as independent of each other as main ( ). Most functions
depend on other functions to some extent. For example, it does not make sense to call the pow
(raise to the pow) function without specifying values for the base and exponent. Moreover, pow is
not useful unless it sends back the result of the computation.
Function can be used in the four variations:
1. function that does not need any arguments and also returns no value.
2. Function that does not need any argument but returns a value.
3. Function that needs one or more arguments but returns no value.
4. Function that needs one or more arguments and also returns a value.
#include<stdio.h>
#include<conio.h>