C language foundation - function (below)

Foreword: The content of this issue will continue to explain the knowledge of functions by taking over the content of the previous issue of C language foundation - function (part one) .

Table of contents

1. Function nested call and chain access

1. Nested calls

2. Chain access

 2. Function declaration and definition

1. Function declaration

2. Definition of function

3. Function recursion

1. What is recursion

2. Two necessary conditions for recursion

Four. Summary


1. Function nested call and chain access

1. Nested calls

Functions can be combined according to actual needs, that is to say, when using a function, you can call another function in its function body , which is nested calling .

#include<stdio.h>
void fun1()
{
    printf("hehe\n");
}
void fun2()
{
    printf("haha\n");
    fun1();
}
int main()
{
    fun2();
    return 0;
}

Let's look at such a simple code, we call the function fun1 in the function fun2 , and then call the function fun2 in the main function , the result is as follows:

 Both "hehe" and "haha" are printed, which is the nested call.

It should be noted here that although functions can be called nestedly, they cannot be defined nestedly , that is to say, another function cannot be defined in one function.

#include<stdio.h>

void fun2()
{
    void fun1()
        {
            printf("hehe\n");
        }
    printf("haha\n");
}
int main()
{
    fun2();
    return 0;
}

Code like the above is absolutely not allowed , which is the same as not being able to define functions in the main function .

2. Chain access

Using the return value of a function (function tail) as the parameter of another function (function head), we can understand it as connecting several functions together in order from beginning to end, like a function chain , this is the chain visit .

Let's take a common example to help you understand:

#include<stdio.h>
#include<string.h>
int main()
{
	int arr[] = "1234567";
	int len = strlen(arr);
	printf("%d", len);
	return 0;
}
-------------------------------------------------------------------------------------------
int main()
{
	int arr[] = "1234567";
	printf("%d", strlen(arr));
	return 0;
}

Both the strlen() function and the printf() function are commonly used functions. Let’s look at the first piece of code first. We define the integer variable len to receive the return value of the strlen() function , and then len is used as the printf() function Arguments are printed .

Looking at the second piece of code again, we did not define any variable to receive the return value of the strlen() function, but directly used strlen(arr) as a parameter of the printf() function .

In fact, strlen(arr) is the corresponding return value of the strlen() function , but we are usually used to define a new variable to receive.

 2. Function declaration and definition

1. Function declaration

  • Tell the compiler what the function is called, what the parameters are, and what the return value is. This is the declaration of the function.
  • Function declarations come before use.
#include<stdio.h>
int main()
{
	int a = 2;
	int b = 5;
	Add(a, b);
	return 0;
}
void Add(int x, int y)
{
	printf("%d", x + y);
}

In the code we wrote in the previous explanation, the custom function is in front of the main function , but now we put it behind the main function , will there be any difference? ? ?

 

 Unfortunately, this approach is wrong.

We know that the compiler reads the code line by line from top to bottom . For the above code, because the Add function is after the main function, when the compiler calls the Add function in the main function, the compiler does not recognize the Add function , so just An error has occurred.

But if this is the case, must the custom function be after the main function? ? ? of course not:

#include<stdio.h>
void Add(int x, int y);//提前声明
int main()
{
    int a = 2;
    int b = 5;
    Add(a, b);
    return 0;
}
void Add(int x, int y)
{
    printf("%d", x + y);
}

We only need to declare the Add function before the main function , and it can be used normally. The declaration here only needs the header of the function , so that the compiler can remember the Add function, and don't forget to add ';' at the end .

2. Definition of function

Specifically explain what a function is structured and what it does, this is the definition of a function.

void Add(int x, int y)
{
	printf("%d", x + y);
}

This is the definition of a simple addition function.

3. Function recursion

1. What is recursion

To put it simply, recursion is a function that nests and calls itself, that is, nesting dolls . The idea is to make big things into small things .

Let me show you the simplest recursive code in history: 

#include<stdio.h>
int main()
{
	printf("hehe\n");
	main();
	return 0;
}

The result is as follows:

 It will print "hehe" endlessly.

2. Two necessary conditions for recursion

  • In fact, function recursion is also similar to a loop, and there are restrictions . When this restriction is met, the recursion will not continue.
  • After each recursive call, it gets closer and closer to this limit .

 Let's formally use an example to explain function recursion in detail:

Takes an integer value (unsigned) and prints out each bit of it in order .

For example: input: 1234, output: 1 2 3 4.

If we write this code normally without recursion, we only need to judge the number of digits first , and then use rolling and dividing to get the quotient to realize it . The code is as follows, just for understanding:

#include<stdio.h>
int main()
{
	int n;
	scanf("%d", &n);
	int count = 1;
	int z = n;
	while (z > 10)//循环统计位数
	{
		z /= 10;
		count *= 10;
	}
	while (count > 0)//辗转相除取商
	{
		int x = n / count;
		n = n % count;
		count /= 10;
		printf("%d ", x);
	}
		return 0;
}

We can see that although it is not difficult to write normally, the code is a bit too cumbersome , so how is recursion realized?

#include<stdio.h>
int print(int n)
{
	if (n > 9)
	{
		print(n / 10);
	}
	printf("%d ", n % 10);
}
int main()
{
	int n;
	scanf("%d", &n);
	print(n);
	return 0;
}

Let's explain in detail how this recursive code works.

First, we enter an integer: 1234

Into the function, n > 9 is a restriction

If n > 9 then we call the function recursively and pass the quotient of n / 10 as a parameter

We don't stop nesting until n == 1, and then start printing the remainder of n % 10 from the inside out

If you still don't understand it, let's see what I'm going to do next:

#include<stdio.h>
int print(int n)// n = 1234
{
	if (n > 9)
	{
		int print(int n / 10) //n = 123
		{
			if (n > 9)
			{
				int print(int n/ 10)//n = 12
				{
					if (n > 9)
					{
						int print(int n / 10) //n = 1
						{
							if (n > 9)//不满足
							{
								print(n / 10);//不执行
							}
							printf("%d ", n % 10);//打印1
					}
					printf("%d ", n % 10);//打印2
			}
			printf("%d ", n % 10);//打印3
	}
	printf("%d ", n % 10);//打印4
}

If it is in this form, will you be enlightened? Hahaha, yes, in fact, the essence is this kind of nesting doll , but we can’t write code like this, one is redundant, and the other is that the compiler cannot pass it .

Four. Summary

Since function recursion is an important point in the knowledge of C language, it is also a difficult point, so the blogger will not explain too much here, and I will spare time to do an article to explain function recursion in detail, so stay tuned!

Well, the knowledge about functions is coming to an end here, friends who like blogger’s articles, don’t forget to click three times !

See you next time!

Guess you like

Origin blog.csdn.net/2303_78442132/article/details/132395198