Name of function cannot be global identifier: Example

  
In c programming language name of function cannot be global identifiers. We function name will global identifiers then it will cause of compilation error.

(1) Program to find area of any circle.

#include <stdio.h>
#include <math.h>
float __radius__(float);

int main(){
   float r,a;
   printf("Radius of circle ");
   scanf("%f", &r);
   a=__radius__(r);
   printf("%f\n", a);
   return 0;
}
float __radius__(float r){
   float a;
   a = M_PI * r * r;
   return a;
}

Above program is valid in c language. But it is bad practice to write the function name like __radius__ since this format is used for global identifiers.

(2) Program to find LCM of any two numbers.

#include <stdio.h>
int main(){
   int n1,n2,lcm;
   printf("\nEnter two numbers:");
   scanf("%d %d",&n1,&n2);
   lcm=__TIME__(n1,n2);
   printf("L.C.M=%d",lcm);
   return 0;
}
int __TIME__(int n1,int n2){
   int x,y;
   x=n1,y=n2;
   while(n1!=n2){
      if(n1>n2)
        n1=n1-n2;
      else
        n2=n2-n1;
   }
   return x*y/n1;
}

Output: error
Explanation: Function name cannot be global identifier.

No comments: