Abeer Abeer - 1 month ago 5
C Question

can an inner function use variables defined in the parent function?

Can a variable be used in an inner function if it was defined in the outer function? or should i modify the inner loop to take a third parameter which would be the array?

Example of the code i'm asking about?

perm_rec1 ( int nr_value , int N)
{
int array[];
secondFunction(int nr_value, int N)
}


The inner function:

secondFunction (int nr_value, int N)
{
int temp = array; //is This possible? Or third parameter?

}

Answer

Can a variable be used in an inner function if it was defined in the outer function?

C does not have nested functions, which is the only sense in which the "inner" and "outer" designations would make sense. When one function calls another, as in your example, it is the call that is "inside" the first function, not the called function.

In any event, variables declared at block scope, such as in your example, are visible only within the remainder of the lexical extent of the declaring block. They are not visible in the body of a function called from within that block -- not even if it is the same function, called recursively.

or should i modify the inner loop to take a third parameter which would be the array?

The most natural implementation would be, yes, to give the function a third parameter by which to provide (a pointer to the first element of) the array to that function.