Function declaration and function definition in c:
1. If function definition has written after the function call then it is necessary to declare the function before the function call because function call statement has no idea about prototype of calling function.
Example 1:
#include<stdio.h>
#include<stdio.h>
float sachin(int x){
float r=(float)x;
return r;
}
int main(){
float f;
f=sachin(33);
printf("%f",f);
return 0;
}
Output : 33.000000
Example 2:
#include<stdio.h>
Example 2:
#include<stdio.h>
int main(){
float f;
f=sachin(33);
printf("%f",f);
return 0;
}
float sachin(int x){
float r=(float)x;
return r;
}
Output : Compilation error
Example 3:
#include<stdio.h>
Example 3:
#include<stdio.h>
float sachin(int);
int main(){
float f;
f=sachin(33);
printf("%f",f);
return 0;
}
float sachin(int x){
float r=(float)x;
return r;
}
Output: 33.000000
2.If function definition has written before the function call statement then it is not necessary to write function declaration.
3.If return type of function is signed int data type then it not necessary to write function declaration even though function definition has written after the function call.
4.Function’s declaration doesn’t reserve any memory space.
5.In declaration statement it is not necessary to write variable name in parameter of function.
Example 1:
typedef float klpd(int,char);
int main(){
float num,num1,num2;
klpd a,b;
num1=a(5,'a');
num2=b(6,'0');
num=num1+num2;
printf("%f",num);
return 0;
}
float a(int x, char y){
x=x+y;
return (float)x;
}
float b(int p,char q){
p=q-p;
return p;
}
Output: 144.000000
Example 2:
#include<stdio.h>
Example 2:
#include<stdio.h>
int main(){
float num,num1,num2;
num1=a(5,'a');
num2=b(6,'0');
num=num1+num2;
printf("%f",num);
return 0;
}
float a(int x, char y){
x=x+y;
return (float)x;
}
float b(int p,char q){
p=q-p;
return p;
}
Output: Compilation error
No comments:
Post a Comment