Thursday, August 27, 2020

Function in C

Function play an important role in programming.It is basically a collection of statements which perform specific task.Each function have some specific name which is used for calling the purpose in any program.We can say that it is a type of sub-program.

Function is a self contain program that can perform some specific and well define task. A large program is broken down into number of smaller program in the form of function which can perform specific task. we can achieve re-usability by creating functions.Any function can define once and call many times in a program.

In C programming their are two type of functions such as:

 Predefined functions

✔ User define functions.

BUILT-IN FUNCTION

PREDEFINED FUNCTION

Predefined functions are built-in functions or library functions which is used by user.User can not define those functions.These functions are created at the time of creation of C language.We cannot change their definition.These functions are stored in library files and these library files are known as header files. We can identify these file by ( .h ) extension.There are so many header files are present in C.If we want to use any built-in function in our program than we must include these header files in C program by using INCLUDE keyword. 

like: # include <conio.h> , # include <stdio.h> , # include <math.h> , # include <string.h> etc.

Now we can simply say that all built-in function are available in header files. So without including these header file we can not use any built-in functions in any program.

Some built-in functions are printf() , scanf() , sum() , pow() etc

USER DEFINE FUNCTION:

User define function are those function which is define by user for their specific task.Let we understand Why user define function needed ?

We know that we write our program inside main() function. If our program is small than no need to create any functions but if our program goes long in size line of code than we must break our program in small modules called function.

This process will improve :

✔ Clarity  [ it is very clear to see every line of code ]

✔ Readability  [ things are readable so that we can understand ]

✔ Re-usability  [ if we need any function again and again in same program ]


For creating any function we must do following:

  1. Function Declaration
  2. Function Definition
  3. Arguments of a function.
  4. Return statement.

1. Function Declaration

Every programmer must define function before it use in program.We must define this function return which type of value like int, float, char etc. and list of argument used in program.

Syntax: 

Data_Type function_name (Parameter_List);

Example:

float area (float a ,float b);

int volume (int a, float b,  float c);

char function_name(arg 1, arg 2..);

void show ( );

All above cases show , Data type may be (char , int , float etc.) which show return type of any function.

2. Function Definition:

For using function in any program than we must define function.Function definition contain name of function , number of arguments (0,1,2...) and body of function.

Syntax:

Type Function_name (arguments....)

{

    body of function

    return statement.

}

Example:

float cube (float x)

{

    return (x * x * x);

}

3. Arguments of a function:

In any function argument are placed inside the parenthesis  ( ). Inside parenthesis number of argument may be [ 0 , 1 , 2 , 3 ,.....,n ]. This argument list contain valid datatype and variable name such as  [ int a , float b , char c..] . variables are medium through which calling function communicate to called function in any program.

Let we explain with the help of example:

# include <stdio.h>

main( )

{

    int a;

    for ( a =1; a <= 5; a ++ )

        {

            printf( " /n cube of %d" is %d ", a , cube (a)) ;    Calling Function

        }

    }

    cube ( int x )        Called Function

    {

        int y ;

        y = ( x * x * x ) ;

        return (y) ;

        }

Explanation :

Here in the above example we create cube() function with argument (a) so here one argument is used.We can use more than one arguments according to function requirement.In calling function argument (a) is called actual argument and in called function (int x) is called formal argument. Actual argument in calling function and formal argument in called function must be same datatype .Calling function and called function must have same number of arguments.

4. Return Statement :

Called function return one value to calling function.There are number of arguments in called function but it return single value.Return statement is responsible to return value from called function to calling function.Our main purpose of create any function is to produce some value after processing statements inside the function.

Like above example called function produce cube of x , process it and result is return to calling function.

Example:

 type function_name ( data_type variable_name )        

    {

       ----------

        ---------

        return (value) ;

        }

Complete Program:

#include < conio.h >

#include < stdio.h >

void main ( )

{

int i;

clrscr ( );

int cube ( int n );     // Function declaration

ptintf ( "Enter any number:" );

scanf ( "%d",&n);

printf ( "Cube of %d is %d",n,cube ( n ) );   // Calling function

getch ( );

}

cube ( int a )          //Called function

{

int y;

y=a*a*a;

return ( y );

}

I hope you all understand the basic of function,how we can create a simple function in any C program.

No comments:

Post a Comment