What happens when a variable is not declared in function definition?

Generally in C program the function definition and calling takes the form as given below:

main()
{
int x,y,z;
z=sample(x,y);
printf(“%d”,z);
}

sample(x1,y1)
int x1,y1;
{
int z1;
z1= x1 – y1;
return(z1);
}

Here what happens is the values x, y gets passed to x1,y1 and the value z1 is calculated in sample function and the value z1 is returned which is got in z. This value namely z is then printed.

Now let us see if the variables are not declared before the braces in the function and if it declared as below:

main()
{
int x=10;
int z;
z=sample(x);
printf(“%d”,z);
}

sample(x1)
{
int x1;
int z1;
z1= x1+20;
return(z1);
}

This would give an error. Surprised!, Yes because the variable x1 is not declared before the braces of the function and so by default would be assumed to be a integer variable. Again the declaration of integer variable x1 inside the function braces would give an error as Redeclaration of variable x1

Hence care must be taken not to do as above.

Editorial Team at Geekinterview is a team of HR and Career Advice members led by Chandra Vennapoosa.

Editorial Team – who has written posts on Online Learning.


Pin It

Leave a Reply