Important components in C language - functions (2)

Introduction: Connecting to the last function (1) ( http://t.csdn.cn/Xc5Ej ), we have initially understood and learned the functions in C language. Today we continue to explore functions.


Table of contents

1. Nested calls and chained access of functions

2. Declaration and definition of functions 

 3. Recursion of functions


1. Nested calls and chained access of functions

Nested function calling:
In C language, nested function calling can realize the calling relationship between multiple functions. When a function internally calls another function, it can be considered as part of an expression or statement. Nested function calls follow the stack principle, that is, each function call creates a new function stack frame.

The following is an example of nested function calls:

#include <stdio.h>

void funcC() {
    printf("This is function C\n");
}

void funcB() {
    printf("This is function B\n");
    funcC(); // 嵌套调用函数C
}

void funcA() {
    printf("This is function A\n");
    funcB(); // 嵌套调用函数B
}

int main() {
    funcA(); // 嵌套调用函数A
    return 0;
}

In the above code, the function funcAcalls the function funcB, funcBcalls the function again funcC, and finally maincalls the function within the function funcA. The execution process of the program is that main -> funcA -> funcB -> funcCeach call will enter a new function stack frame, and after execution is completed, it will return to the previous function stack frame.

It is worth noting that functions can be called nested, but definitions cannot be nested.

Chained access to functions:

Use the return value of one function as a parameter of another function

#include <stdio.h>
#include<string.h>
int main(){
	char arr1[20] = {0};
	char arr2[] = "abc";
	printf("%d\n", strlen(strcpy(arr1, arr2)));//链式访问
}

In the above code, the return value of the strcpy function is the parameter of strlen, and the return value of strlen is the parameter of the printf function. This is called chained access of the function.


2. Declaration and definition of functions 

function declaration

1. The declaration refers to telling the compiler that there is a function (including what the function is called, parameters, and return type), but whether it exists or not cannot be determined by the declaration.

2. The declaration of a function is generally before the use of the function, and it must be declared first and then used .

3. The function declaration is generally placed in the header file.

function definition

The definition of a function generally refers to the realization of the function's function.

In future projects, there will generally be a lot of code, so we generally place the declaration of the function in the .h file, and the implementation of the function in the .c file.


 3. Recursion of functions

1. What is recursion?

The programming technique in which a program calls itself is called recursion

Recursion as an algorithm is widely used in programming languages. A process or function has a method of directly or indirectly calling itself in its definition or description. It usually transforms a large and complex problem into a smaller problem similar to the original problem to solve. The recursive strategy only A small number of programs are needed to describe the multiple repeated calculations required in the problem-solving process, which greatly reduces the amount of program code.

The main thinking about recursion: making big things small

2. Two necessary conditions for recursion

  • Restrictions must allow the function to eventually stop
  • Approaching conditions, making each recursion closer to the limiting conditions

 example:

 Summary: The above is what I have brought you today to give you a further understanding of functions. Thank you for watching!!!

Guess you like

Origin blog.csdn.net/2301_77125473/article/details/132250676