Function In C Language


What is Function? Explain different types of functions used in c Language.
Function is a block of code that perform a specific task,
C function are of two types

  •    Library functions or inbuilt functions
  •    User Defined Functions (UDF)

Library Functions:

Library functions are those functions which are already written, compiled and kept within a library of c language. There are thousands of library functions and when we require call that function in our program.

Examples of Library Function are
printf() , scanf(), strlen(), strcat(), strcmp(), strcpy(), sqrt() etc

User Defined Function:

User defined function are those function which are created by the user. These are the function which are written by the user at the time of writing the program.
User defined function mainly consists of the three elements/parts. They are

  •           Function declaration
  •           Function definition
  •           Function call

Examples of user defined functions are
void main(), void addition(), int prime(), int max(int,int)

Explain Function declaration, function definition and function call with example.

Function Declaration:
Like variables, function in C must be declared before they are invoked. A function declaration/prototype consists of four parts.
  •        Function type (return type)
  •          Function name
  •        Argument list
  •        Terminating semicolon (;)

The General format is

return_typefunction_name (argument list);

examples:
void mul (int, int);
int mul (int, int);
void mul (int a, int b);
void mul ( );

Function declaration can be done at two places in program.
  •       Before the main(), the global declaration-
  •       Within main (), the local declaration

Function Call:
A function can be called using the function name followed by the list of parameters (arguments) if any and return type as per the declaration of the function.
When a compiler encounter a function call, the control is transferred to the function definition for execution and then the control is return back to the calling statement.

void function1();
void function2();
main( )
{
 ……………
 ……………
function1( );    // this is function1 call , so control will be transferred to function1 definition
……………
……………
……………
function2( );    // this is function2 call , so control will be transferred to function2 definition
……………
……………
……………
}

Void function1( )
{
……………
……………
……………
}           // after executing function1 control will be transferred to calling function ie function1.

Void function2( )
{
……………
……………
……………
}           // after executing function2 control will be transferred to calling function ie function2.
Function Definition:

Function definition include the following elements.

  •        Function type (return type)
  •        Function name
  •        Argument list
  •        Local variable declaration
  •        Statements
  •        return statement

return statement is optional based on the declaration  of the function.
All the above elements are group into two parts

  •        Function header
  •        Function body

A general format of function definition is

return_typefunction_name (parameter list)
{
local declarations;
executable statements;
……………………..;
……………………..;

return statements;
}

Example:
Write a c program to find the maximum of two numbers using function.
#include<stdio.h>

int maximum (int, int);             // function declaration
void main()
{
            int a, b, max;
            printf(“Enter any two value”);
            scanf(“%d%d”,&a,&b);
            max=maximum(a,b);   //function call
            printf(“Greater number=%d\n”,max);
}

int maximum(int x, int y)         // function definition
{
if(x>y)
return (x);
else
return (y);
}

What is user defined function? Explain actual argument and formal argument.

Function is the block of code that perform the specific task.

Actual Argument:

  •        Actual argument are the arguments which are passed within function call statement.
  •        Actual argument can be variable name, expression or constants
  •        Values must be assigned to the variables of actual argument before function call is made.

Formal Argument:

  •       Formal argument are the arguments in the function definition. Formal argument must be a valid variable name. Formal argument receives the value of actual argument.
  •       If the Actual argument are less than formal argument, the remaining formal argument will be assigned with garbage values.
  •       If the actual argument are more than the formal argument, they are discarded.


main( )
{
 ……………
fun1( a1, a2, a3, ….., an);      // this is function1 call , actual argument
……………
}

fun1( b1, b2, b3, ….., bn)                    // function1 definition , formal argument
{
……………
……………
}

During function call only the copy of values of actual argument is passed, which are received by the formal argument variable in the function definition. Hence any change made in formal argument will affect the values of actual argument.

Sr. No.
Actual Argument
Formal Argument
1
Arguments passed in the function call statement are actual argument
Arguments in the function definition are the formal argument
2
Actual argument can be variable, expression or constant
Formal argument must be a valid variable
3
Example: void addition(a,b);
                void addition(10,20);
void addition(int x, int y) { }






List out the categories of function and explain the various category with example.



Depending upon the argument passing and returning, the function is classified into four categories.

  •        Function with no arguments and no return values
  •        Function with no arguments and return values
  •        Function with arguments and no return values
  •        Function with  arguments and  return values


Function with no arguments and no return values

Here the function has no argument, hence no parameter are passed while calling and at the same time function has no return type (means void), and hence nothing is returned from the function to the called function.
Example:
void maximum ( );                   // function declaration
void main ( )

{
clrscr ();
maximum ( );                           //function call
getch ();
}

void maximum ( )                    //function definition
{
int a, b;
printf (“enter the value of a and b”);
scanf (“%d%d”,&a,&b);
if ( a > b)
            printf (“Max=%d”, a);
else
            printf (“Max=%d”, b);
}


 Function with no arguments and return values

Here the function has no argument, hence no parameters are passed while calling but the function return values, hence return type must be specified in the function( return type is int)
Example:
int maximum ( );                                  // function declaration
void main ( )
{
int m;
clrscr ();
m=maximum ( );                                  //function call
printf (“Max=%d”, m);
getch ();
}

int maximum ( )                                               //function definition
{
int a, b;
printf (“enter the value of a and b”);
scanf (“%d%d”,&a,&b);
if (a>b)
            return ( a);
else
            return ( a);
}

Function with arguments and no return values

Here the function has argument, hence parameter are passed while calling but the function has no return type (means void), and hence nothing is returned from the function to the called function.
Example:

void maximum ( int, int );                    // function declaration
void main ( )
{
int a, b;
clrscr ();
printf (“enter the value of a and b”);
scanf (“%d%d”,&a,&b);
maximum (int, int );                             //function call
getch ();
}
void maximum (int x, int y )                //function definition
{
if ( x > y)
            printf (“Max=%d”, x);

else
            printf (“Max=%d”, x);
}


Function with  arguments and  return values

Here the function has argument, hence parameter are passed while calling and at the same time  the function return values, hence return type must be specified in the function( return type is int)
int maximum (int, int);                         // function declaration
void main ( )
{
int a,b,m;
clrscr ();
printf (“enter the value of a and b”);
scanf (“%d%d”,&a,&b);
m=maximum ( a, b);                            //function call
printf (“Max=%d”, m);
getch ();
}

int maximum (int x, int y )                   //function definition
{
if (x>y)
            return (x);
else
            return ( y);
}

Comments

Popular posts from this blog

History, Basic Structure, C Token, Data Type, operators, printf() and scanf()

Practical 1