Learn C Function by Example 4 : Function name cannot be same of identifier within the scope



Invalid Example :

#include<stdio.h>
int sum(int,int);
int main()
{
   int sum,num1,num2;
   num1 = 10;
   num2 = 20;
   sum = sum(num1,num2);
   printf("Sum is : %d",sum);
   return 0;
}
int sum(int n1,int n2)
{
  return (n1+n2);
}

Output :

Compile Error

Explanation :

  1. Consider following snippet
{
   int sum,num1,num2;
   num1 = 10;
   num2 = 20;
   sum = sum(num1,num2);
}
  1. Opening and Closing curly braces is considered as one block.

Point 3 : Here inside same block we have two identifiers
[arrowlist]

  • “sum” as Variable name
  • “sum” as Function name
[/arrowlist]
  1. So it will cause confusion of compiler whether to treat “sum” as variable or function name.