Friday, 20 October 2017

Function

A function is a combination of more than one statement which execute together one by one to achieved some specific task. Function code run while calling. There are many variety of functions some takes arguments and return something or takes argument and doesn’t return and more.

C language is known as building block of function all the things of programming is achieved through function. Program gets start running with main function.

Types


No argument, No return


This type of function neither returns anything nor takes arguments. When a function doesn’t have something to return then void is used. Void indicates no return.

no return type function

Example

void msg(void);
void main()
{
     int a,b,c;
     msg();
}
void msg()
{
     printf("\n\tGirfa Student Help : C Language tutorial");
}

Argument, No Return


This type of function takes argument but doesn’t return something.

 

Argument, No Return

Example



void sum(int,int);
void main()
{
     int a,b;
     printf("Enter two number>> ");
     scanf("%d%d",&a,&b);
     sum(a,b);

}
void sum(int a,int b)
{
     printf("\n\tSum=%d",a+b);
}

Return,No Argument

Function return but doen’t takes argument

 


Return,No Argument





Example

int sum(void);
void main()
{
     printf("\t\n%d",sum());

}
int sum()
{
     int a,b,c;
     printf("Enter two number>> ");
     scanf("%d%d",&a,&b);
     c=a+b;
     return(c);
}

Argument with return

Function takes argument and return result. 

Argument with return



Example

int sum(int,int);
void main()
{
     int a,b,c;
     printf("Enter two number>> ");
     scanf("%d%d",&a,&b);
     printf("\t\n%d",sum(a,b));

}
int sum(int a,int b)
{

     return(a+b);