Monday 21 December 2015

FUNCTIONS

           FUNCTIONS
WHAT IS A FUNCTION
Functions are "self contained" modules of code that accomplish a specific task. Functions usually "take in" data, process it, and "return" a result. Once a function is written, it can be used over and over and over again. Functions can be "called" from the inside of other functions.

Types of functions:
There are 2 types of functions:
                                Built-in functions
                                User defined functions
Built-in functions:
These are those functions which are a part of the core of a language .the functions cannot be redefined for any other purpose.They are contained in the library.to use a built its header file must be mentioned at the top of the program
User Defined Functions:
The functions which is defined by the user is called user defined function
·       There are 3 parts of a function:
1.   Function header
2.   Function calling
3.   Function defintion
Function header
Function header consists of the return type, name of function and the data type and name of arguments of a function the arguments specified in function header are dummy arguments’it is terminated by a semicolon.
SYNTAX:
return_type function_name(type(1) argument(1),....,type(n) argument(n));

Function definition :
It is the part where the actual code of the function is written. It has 3 parts:
1.   Function prototype
2.   function body
3.    Return statement

Function prototype(declaration):
Every function in C programming should be declared before they are used. These type of declaration are also called function prototype. Function prototype gives compiler information about function name, type of arguments to be passed and return type.Function declarator is the first line of function definition. When a function is called, control of the program is transferred to function declarator.
Syntax of function declarator

return_type function_name(type(1) argument(1),....,type(n) argument(n))
Syntax of function declaration and declarator are almost same except, there is no semicolon at the end of declarator and function declarator is followed by function body.
2. Function body
Function declarator is followed by body of function inside braces.
Passing arguments to functions
In programming, argument(parameter) refers to data this is passed to function(function definition) while calling function.
Return statement:
It is the last statement of function body
It is used if the return type is not void if its void its not used
3. CALLING A FUNCTION:
A function is called in the body of main
Format:


Function_name(argument1,argument2,….);
Function is called in the main function by writing its name followed by parameters sequentially







No comments:

Post a Comment