I have a function,and a matrix declared above it
The matrix and the function are both declared in the same declarative region (it seems in the global namespace). So as the matrix is declared before the function definition then the matrix declaration is visible in the body of the function.
That is according to the unqualified name lookup (The C++ 17 Standard, 6.4.1 Unqualified name lookup)
6 In the definition of a function that is a member of namespace N, a
name used after the function’s declarator-id shall be declared before
its use in the block in which it is used or in one of its enclosing
blocks (9.3) or shall be declared before its use in namespace N or, if
N is a nested namespace, shall be declared before its use in one of
N’s enclosing namespaces.
If you will exchange the matrix declaration and the function definition like
int function(int row , int col)
{
if (M[row][col] == 1)return 1;
return 0;
}
int M[100][100];
then the compiler will issue an error because the name M
used in the function body will not be found before its usage.