First, check that neither main. c, test () nor add () are declared, but no error is reported during compilation.
[Html]
# Include <stdio. h>
Void main ()
{
Printf ("% d \ n", test ());
}
Int test ()
{
Return add (1, 2 );
}
Int add (int x, int y)
{
Return x + y;
}
# Include <stdio. h>
Void main ()
{
Printf ("% d \ n", test ());
}
Int test ()
{
Return add (1, 2 );
}
Int add (int x, int y)
{
Return x + y;
}
Output result: 3
Next, modify the program as follows:
[Html]
# Include <stdio. h>
Void main ()
{
Printf ("% d \ n", test ());
}
Static int test ()
{
Return add (1, 2 );
}
Static int add (int x, int y)
{
Return x + y;
}
# Include <stdio. h>
Void main ()
{
Printf ("% d \ n", test ());
}
Static int test ()
{
Return add (1, 2 );
}
Static int add (int x, int y)
{
Return x + y;
}
An error is reported during compilation, indicating that test () and add () are not declared.
Why does it look like this? So I tried to modify the program again:
[Html]
# Include <stdio. h>
Void main ()
{
Test ();
Void test ()
{
// Return add (1, 2 );
Add (1, 2 );
}
Void add (int x, int y)
{
// Return x + y;
X + y;
}
# Include <stdio. h>
Void main ()
{
Test ();
Void test ()
{
// Return add (1, 2 );
Add (1, 2 );
}
Void add (int x, int y)
{
// Return x + y;
X + y;
}
This error is reported. Note that, compared with the first example, the function returns a different value!
Then the function declaration is given first:
[Html]
# Include <stdio. h>
Void test ();
Void add (int x, int y );
Void main ()
{
Test ();
}
Void test ()
{
// Return add (1, 2 );
Add (1, 2 );
}
Void add (int x, int y)
{
// Return x + y;
X + y;
}
# Include <stdio. h>
Void test ();
Void add (int x, int y );
Void main ()
{
Test ();
}
Void test ()
{
// Return add (1, 2 );
Add (1, 2 );
}
Void add (int x, int y)
{
// Return x + y;
X + y;
}
Compilation successful.
Summary:
1. Generally, when a function calls a subfunction, it must be declared first. (Generally, the function declaration is put in the header file)
2. If the subfunction returns an int value, you do not need to declare it. Because the compiler defaults a declaration for the subfunction and the return value is of the int type, no error will be reported in the first example.
3. The static function scope is from the declaration/definition to the end of the source file.