Learn programming for free
Function in C language.
Function is basically a block of statement which is used to perform some specific task.
In c language we have 4 type of function.When we declare a function then it tells the compiler about return type of function,parameter of function and it's function name.
In C language we divide large program into blocks and call them as and when they required.
We will discuss function in depth in this blog.
Types of function.
- Function without parameter and without return type.
- Function without parameter and with return type.
- Function with parameter and without return type.
- Function with parameter and with return type.
We will discuss each of them with very basic example:
#include<stdio.h>
void sum()
{
int a,b,sum=0;
printf("Enter first number");
scanf("%d",&a);
printf("Enter second number");
scanf("%d",&b);
sum = a+b;
printf("Sum of two number is %d",sum);
}
int main()
{
sum();
return 0;
}
2.Function without parameter and with return type:
#include<stdio.h>
int sum()
{
int a,b,sum=0;
printf("Enter first number");
scanf("%d",&a);
printf("Enter second number");
scanf("%d",&b);
sum = a+b;
return sum;
}
int main()
{
int x = sum();
printf("Sum of number is: %d",x);
return 0;
}
3.Function with parameter and without return type:
#include<stdio.h>
void sum(int a,int b)
{
int sum = a+b;
printf("Sum of number is %d",sum);
}
int main()
{
int a,b;
printf("Enter first number");
scanf("%d",&a);
printf("Enter second number");
scanf("%d",&b);
sum(a,b);
return 0;
}
4.Function with parameter and with return type:
#include<stdio.h>
int sum(int a,int b)
{
int sum = a+b;
return sum;
}
int main()
{
int a,b,x = 0;
printf("Enter first number");
scanf("%d",&a);
printf("Enter second number");
scanf("%d",&b);
x = sum(a,b);
printf("Sum of number is %d",x);
return 0;
}
Do you want to learn c language in depth then visit: learn c language in depth.
0 Comment to "Function in C language."
Post a Comment
If you have any doubts then let me know.