Monday, 30 September 2013

how to is any variable accessed outside the block when "return" is used?

how to is any variable accessed outside the block when "return" is used?

i am beginner in c and have started writing code in c.I am having doubt
regarding scope of variable .when any variable is written inside the block
then its scope is inside that block.but when return word is used how is
accessed outside the block?
for eg:
int add(int a, int b)
{
int c;//scope of c is within this block
c=a+b;
return c;
}//it ends here
void main()
{
int answer;
answer=add(2,3);//how we gets value of "c " here
printf("%d",answer);
}

No comments:

Post a Comment