Home>Article> Can functions be nested in definition but not in nested calls?

Can functions be nested in definition but not in nested calls?

青灯夜游
青灯夜游 Original
2021-02-02 16:28:42 34147browse

Wrong, functions can be nested calls but cannot be nested defined. In C language, all functions are parallel, that is, they are independent of each other when defining functions. One function is not subordinate to another function, that is, functions cannot be nested in definition, but they can call each other, but they cannot call the main function. .

Can functions be nested in definition but not in nested calls?

The operating environment of this tutorial: Windows 7 system, C99 version, Dell G3 computer.

Wrong, functions can be nested calls but cannot be nested defined.

C Language Function

A function is a piece of code that can be reused to independently complete a certain function. It can Receive data passed by the user, or not. Functions that receive user data must specify parameters when defining them. Functions that do not receive user data do not need to be specified. Based on this, functions can be divided into parameterized functions and parameterless functions.

The process of encapsulating a code segment into a function is called function definition.

Definition of function

If the function does not receive data passed by the user, it can be defined without parameters. As shown below:

dataType functionName(){ //body }

If the function needs to receive data passed by the user, then it must be defined with parameters. As shown below:

dataType functionName( dataType1 param1, dataType2 param2 ... ){ //body }
  • dataTypeis the return value type, which can be any data type in C language, such as int, float, char, etc.

  • functionNameis the function name, which is a type of identifier. The naming rules are the same as identifiers. The parentheses ( ) after the function name cannot be missing.

  • dataType1 param1, dataType2 param2...is the parameter list. A function can have only one parameter or multiple parameters, separated by . Parameters are essentially variables, and the type and name must be specified when defining. Compared with the definition of a parameterless function, the definition of a parameterized function only has one more parameter list.

  • bodyis the function body, which is the code that the function needs to execute and is the main part of the function. Even if there is only one statement, the function body must be surrounded by { }.

  • If there is a return value, use the return statement in the function body to return it. The type of data returned must be the same as dataType.

    return is a keyword in C language, which can only be used in functions to return processing results.

Example:

#include  int sum(){ int i, sum=0; for(i=1; i<=100; i++){ sum+=i; } return sum; } int main(){ int a = sum(); printf("The sum is %d\n", a); return 0; }

Run result:

The sum is 5050

Function cannot be nested definition, main is also a function definition, so sum should be placed outside main. Functions must be defined first and then used, so sum must be placed before main.

Note: main is a function definition, not a function call. When the executable file is loaded into the memory, the system starts execution from the main function, that is, the system will call the main function we defined.

Tutorial recommendation: "c language tutorial video"

Function Call

The so-called function call (Function Call ), that is, using an already defined function. The general form of function call is:

functionName(param1, param2, param3 ...);

functionName is the function name, param1, param2, param3... is the actual parameter list. Actual parameters can be constants, variables, expressions, etc. Multiple actual parameters are separated by commas.

In C language, there are many ways to call functions, for example:

//函数作为表达式中的一项出现在表达式中 z = max(x, y); m = n + max(x, y); //函数作为一个单独的语句 printf("%d", a); scanf("%d", &b); //函数作为调用另一个函数时的实参 printf( "%d", max(x, y) ); total( max(x, y), min(m, n) );

Nested calling of functions

Functions cannot be nested definitions , but calls can be nested, that is, a call to another function is allowed during the definition or call of one function.

[Example] Calculate sum = 1! 2! 3! ... (n-1)! n!

Analysis: You can write two functions, one for calculating factorial and one for Used to calculate the cumulative sum.

#include  //求阶乘 long factorial(int n){ int i; long result=1; for(i=1; i<=n; i++){ result *= i; } return result; } // 求累加的和 long sum(long n){ int i; long result = 0; for(i=1; i<=n; i++){ //在定义过程中出现嵌套调用 result += factorial(i); } return result; } int main(){ printf("1!+2!+...+9!+10! = %ld\n", sum(10)); //在调用过程中出现嵌套调用 return 0; }

Operating results:

1!+2!+...+9!+10! = 4037913

The call to factorial() appears in the definition of sum(), the call to sum() appears during the call to printf(), and printf () is also called by main(), and their overall calling relationship is:

main() --> printf() --> sum() --> factorial()

If a function A() calls another function B() during the definition or calling process, then we call it A() is the calling function or main function, and B() is called the called function.

When the calling function encounters the called function, the calling function will pause, and the CPU will execute the code of the called function; after the called function completes execution, it will return to the calling function. The status continues to execute.

The execution process of a C language program can be considered as the mutual calling process between multiple functions, which form a simple or complex calling chain. The starting point of this chain is main(), and the end point is also main(). When main() has finished calling all functions, it will return a value (such as return 0;) to end its own life, thereby ending the entire program.

A function is a code block that can be reused. The CPU will execute the codes one by one. When encountering a function call, the CPU must first record the address of the next code in the current code block (assuming the address is 0X1000), then jump to another code block, and then come back to continue executing the code at 0X1000 after the execution is completed. The whole process is equivalent to the CPU taking a break, temporarily putting down the work in hand to do something else, and then continuing the previous work after finishing it.

For more knowledge about computer programming, please visit:Programming Video! !

The above is the detailed content of Can functions be nested in definition but not in nested calls?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn