January 22, 2013

Function in C-language



function syntax of defining it header in C and C++ language


A function is block of codes that performs a particular task, this block is given a name and whenever we want to execute this block we simply write the name of the function. Function is a small sub-program which is a self contained block of statements and the function which is defined by the user is User Defined Functions.


The function can be called by other function or by the main function by just specifying its name. Function is basically used to implement concept of modularity. It means it divides the bigger problem in smaller sub-parts. A function starts with its return type followed by function name and pair of parenthesis and there after block of statements are used. 
Functions are basically classified as LIBRARY FUNCTIONS and USER DEFINED FUNCTIONS.

Library functions are those which are already defined and stored in different header files to use particular library functions. We simply have to add particular file in our programs like: printf(“___”); or scanf(“___”);  these are basically stored in header file stdio.h, clrscr();  this is basically stored in header file conio.h
      Square root, power, exponential  these are basically stored in header file math.h
User defined functions which is defined by the programmer himself to solve any particular type of problem.

ADVANTAGES OF USING FUNCTION

1. If we don’t use functions the main function becomes very lengthy and the coding becomes very difficult but when we are using function the program can be divided into smaller modules that are easier to manage and can be implemented easily.
2. It becomes easy to isolate and debug the faulty functions.
3. Some operations or calculation may be repeated a number of times in a program. When we are using functions once a function has been defined it can be called in main function any number of times.

Example:

/* Program to show the use of function*/

#include<stdio.h>
#include<conio.h>
void print()
{
     int i;
     for(i=0;i<35;i++)
     {
                      printf("*");
                      }
                      }
void main()
{
     print();
     printf("\n This is the use of functions");
     printf("\n");
     print();
     getch();
     }

output of program to show use of a function in C language


SYNTAX FOR DEFINING A FUNCTION


FUNCTION HEADER

It contains the data type of the value returned by the function name and the pair of parenthesis  () that may be empty or may contain a list of arguments. If the return type is not defined it is assumed to be of type int and if the function doesn’t return anything then return type is specified as void.

A function is given a name by applying rules for naming an identifier. The argument list is a list of parameters passed to a function. These parameters will give values to the function. The different arguments are separated by comma’s (,) or semi-column (;).

FUNCTION BODY

The part of the function enclosed with in braces is known as the function body.
1. Local variable declaration
A function may require some local variables other than arguments. They are declared in this part of section.

2. Executable statements
These are the instruction that will be executed to do particular task.

3. Return statements
It returns the value computed by the function. If function don’t return value then this statement can be omitted.

STORAGE CLASS OF VARIABLES

1. Life time
It is defined as the period during which a variable retains a given value during the execution of programs.

2. Visibility / Scope
It determines the area or portion of the program where a particular variable can be assessed.

3. Storage location
Whether the variables stored in computer memory or in CPU registers.

4. Initial value of variables
Whether it is given some initial value or it picks some garbage value.


TYPES OF FUNCTIONS ARE

1. Functions without parameters.
2. Functions with parameters but with no returning value.
3. Function with parameter with returning value.
4. Prototype functions.
5. Recursive functions.

1 comment:

  1. How I know, functions are a very important element of any programming language. After all, this is the basis for learning to programing well. If we know programming, I think that we can look for work at https://grapeup.com/careers and there will certainly be something for developers .

    ReplyDelete