Function calls can be nested, but function definitions cannot be nested, because in C language, function definitions are parallel and independent of each other, which means that when a function is defined, the function body cannot Contains the definition of another function, that is, functions cannot be nested definitions, but they can be nested calls.
The operating environment of this tutorial: Windows 7 system, C18 version, Dell G3 computer.
In C language, function calls can be nested, but function definitions cannot be nested.
You cannot nest the definition, that is,
function a(){ function b(){ } }
It is wrong to define function b inside function a. You can only define function b outside function a like this
function a(){} function b(){}
Can be nested Set call, that is,
function a (){ b(); }
can call the defined b function in the a function.
The reason why functions cannot be nested is because the syntax is not supported and the function cannot be defined inside the function definition. The definitions of functions in C language are all parallel and independent of each other. That is to say, when a function is defined, the function body cannot contain the definition of another function. That is, functions cannot be nested definitions, but they can be nested calls.
What are nested calls of functions?
In the process of calling a function, the process of calling another function
Function In C language, the definition of each function exists relatively independently, inside the function Other functions can be called (this does not include the main() function). This calling process is called function nesting (the definition part of the function cannot be nested).
Example:
Find the greatest common divisor and least common multiple of two integers.
Problem Analysis
The least common multiple of two numbers = the product of the two numbers/the greatest common divisor of the two numbers.
So the key is the greatest common divisor.
Thinking map for finding the greatest common divisor:
Code implementation
#define _CRT_SECURE_NO_WARNINGS 1 #include"stdio.h" #include"math.h" int gcd(int a, int b) { int c; if (a<b)//保证a的值大于b { c = b; b = a; a = c; } while (a != 0) { c = a%b; b = a; a = c;//把余数赋值给a,直到a=0时跳出循环,找到结果。 } return b; } int lcd(int a, int b) { int c; c = (a*b) / (gcd(a, b));//函数嵌套的过程 return c; } main() { int m, n; printf("请输入两个数:"); scanf("%d,%d", &m, &n); printf("%d和%d最大公约数为%d\n", m, n, gcd(m, n)); printf("最小公倍数为%d\n", lcd(m, n)); }
Implementation of the function: Find the least common multiple and greatest common factor of the two integers 45 and 56.
Recommended: c video tutorial
The above is the detailed content of Can function calls be nested?. For more information, please follow other related articles on the PHP Chinese website!