User Define functions by G Krishna Chauhan


When we write large complex programs, it becomes difficult to keep track of the source code.
The job of functions is to divide the large program to many separate modules based on their functionality.

There are 4 types of functions in C.

  • Functions with no arguments and no return value.
  • Functions with arguments but no return value.
  • Function with no arguments but with return value.
  • Functions with arguments and with return value.

1) Functions with no arguments and no return value in C : 

Syntax :

function_name ()
{
Valid C Statements;
}

Example Program:
#include<stdio.h>
#include<conio.h>
void main()
{
funct();
getch();
}
funct()
{
printf("hi, this is display from function funct()");
}

2) Functions with arguments and no return value in C :

Syntax :

function_name(argument 1, - - - -, argument n)
{
Valid C Statements;
}

Example Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter a value for a :");
scanf("%d",&a);
printf("Enter a value for b :");
scanf("%d",&b);
funct(a,b);
getch();
}
funct(int a1, int b1)
{
int c;
c=a1+b1;
printf("\n Result = %d",c);
}


3) Function with no arguments but with return value in C :

Syntax :

data_type function_name()
{
Valid C Statements;
return(value);
}
Example Program :

#include<stdio.h>
#include<conio.h>
void main()
{
int result;
result=funct();
printf("\n Result = %d",result);
getch();
}
int funct()
{
int a,b,c;
printf("Enter a value for a :");
scanf("%d",&a);
printf("Enter a value for b :");
scanf("%d",&b);
c=a+b;
return(c);
}

4) Functions with arguments and with return value in C :

Syntax :

data_type function_name(arguments)
{
Valid C Statements;
return(value);
}

Example Program :

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,result;
printf("Enter a value for a :");
scanf("%d",&a);
printf("Enter a value for b :");
scanf("%d",&b);
result=funct(a,b);
getch();
}
int funct(int a1, int b1)
{
int c;
c=a1+b1;
printf("\n Result = %d",c);
return(c);
}

No comments:

Post a Comment