Function in C Language :
Function
function is a block of code or set of statement to performs some specific task is called function.
why be use function:
- Reusebility code
- Reduce complexity
- Reduce size of code
- Easily debugging(error find)
- Reduce duplicate
Types of function :
- 1-Predefined function
- 2-User defined function
1- Predefined funtion:
Library functions in C language are inbuilt functions is called
predefined funtion.
- Library function
- Scanf()
- printf()
- clrscr()
- declaration in header files
user defined function are self contained block of statement which
are written by the user to compute or perform a task.
User defined function make a program.
return type function name (list of argument)
int sum(int a ,int b)
No return value
program using void return type
Note :
always return some valueuse return keyword inside functioncan use in expression
example:
#include<stdio.h>
#include<conio.h>
void sum(int a,int b)
{
int c=a+b;
printf(" %d",c);
}
void main()
{
sum(3,5);
getch();
}
output
8
Return value
program using int return type
Note:
- always return some value
- use return keyword inside function
- can use in expression
yes follow three step:
example:
#include<stdio.h>
#include<conio.h>
int sum() // declaration
{
int a=10,b=20;
int c=a+b;
return c;
}
void main()
{
int x=sum();// calling
printf("%d",x);
getch();
}
output
30