Functions in C language (detailed explanation)

Table of contents

1. What is a function

2. Classification of functions in c language:

2.1. Library functions

2. Custom functions

3. Function parameters

3.1 Actual parameters (actual parameters)

3.2 Formal parameters (formal parameters)

4. Function call:

4.1 Call by value

4.2 Call by reference

5. Nested call and chain access of functions 

5.1 Nested calls 

5.2 Chain access

6. Function declaration and definition

6.1 Function declaration:

6.2 Function definition:

7. Function recursion

7.1 What is recursion? 

7.2 Two Necessary Conditions for Recursion 


1. What is a function

In Wikipedia, the definition for a function is a subroutine . A subroutine is a certain part of code in a large program, consisting of one or more statement blocks. It is responsible for completing a specific task and is relatively independent compared to other codes.

Generally, there will be input parameters and a return value, providing the encapsulation of the process and the hiding of details. These codes are usually integrated as software
libraries.

2. Classification of functions in c language:

2.1. Library functions

Why are there library functions?

The early C language did not have functions. It only stipulated your grammar, such as how to write for loops, and the rules for various grammatical details were very clear, that is, the code you wrote could be edited and processed. But one day, when A wants to print a message on the screen, A implements a function similar to printf1. At this time, B said, I also want a printing function, so he wrote a printf2, and then C said that he also wanted to print. As for him, he also wrote a printf3 function.

None of these people know each other, so let's take a look. When each of us implements the function of printing, this code becomes redundant.

What is the second? The development efficiency is low, and each of us is creating theories repeatedly. You write one, he also writes one, and others write another. They are all written with similar functions, and the development efficiency is of course low .

The third is that it is not standard. What you write and what you write are all the same function, but the implementation method may be different. The parameters may also be different, and the return value type may also be different.

So based on the above reasons. Can some commonly used functions be implemented as functions? At this time, there is the concept of a library function, and this function only needs to specify the parameters to death. The return type specification is dead. If the function name is stipulated, then its use method must be exactly the same. The emergence of library functions makes our code development efficiency actually higher. The code is more standardized.

Note: But the use of library functions must include the corresponding header files

Here are two recommended sites to study: cppreference.com

cplusplus.com - The C++ Resources Network

So how to learn library functions?
Here we simply take a look: http://www.cplusplus.com

This is the library of c. We can quickly find the functions we have used in the left part, so I will use an example to take you to learn the library functions

The function strcpy is included inthe header file string.h . The parameters of this function require two pointers , and the return value is a character pointer, which is the address . char * strcpy ( char * destination, const char * source );

Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).

The translation is to copy the C string pointed to by source into the array pointed to by destination, including the terminating null character (and stopping at that point). Include the '\0' character.

Pointer to the destination array where the content is to be copied.

Pointer to the destination array whose contents are to be copied.

C string to be copied.

the c-string to be copied

 The return value is destination, and destination is a character pointer.

To sum up, the strcpy function copies a string to another string.

2. Custom functions

If library functions can do everything, what do programmers need to do? So more important is the custom function. Like library functions, custom functions have function names, return value types, and function parameters. But the difference is that these are all designed by ourselves. This gives programmers a lot of room to play.

The composition of the function:

ret_type fun_name(para1, * )
{ statement;//statement item } ret_type return type fun_name function name para1 function parameter




Give an example and you will know at a glance.

3. Function parameters

3.1 Actual parameters (actual parameters)

The parameters actually passed to the function are called actual parameters. Actual parameters can be: constants, variables, expressions, functions, etc. No matter what type of quantity the actual parameters are, they must have definite values ​​when the function is called, so that these values ​​can be transferred to the formal parameters.

3.2 Formal parameters (formal parameters)

The formal parameter refers to the variable in the parentheses after the function name, because the formal parameter is only instantiated (allocated memory
unit) when the function is called, so it is called the formal parameter. Formal parameters are automatically destroyed when the function call completes. Therefore formal parameters are only valid within the function.

The name of the formal parameter and the actual parameter can be the same, it does not affect anything.

4. Function call:

4.1 Call by value

The formal parameters and actual parameters of the function occupy different memory blocks, and the modification of the formal parameters will not affect the actual parameters.

Write a function that swaps the contents of two integer variables 

void swap(int p1, int p2)
{
	int tmp = 0;
	tmp = p1;
	p1 = p2;
	p2 = tmp;
}
int main()
{
	int a = 0;
	int b = 0;
	scanf("%d%d", &a, &b);
	printf("交换前,a = %d b = %d\n", a, b);
	swap(a, b);
	/*int p1 = &a;
	int p2 = &b;
	swap(p1, p2);*/
	printf("交换后,a = %d b = %d\n", a, b);
	return 0;
}

It can be seen that I have clearly passed the parameters in, why haven't the parameters been exchanged yet? We're about to use the important call-by-reference.

4.2 Call by reference

Call by address is a way to call a function by passing the memory address of the variable created outside the function to the function parameter. This way of passing parameters can establish a real connection between the function and the variables outside the function, that is, the inside of the function can directly manipulate the variables outside the function.

void swap(int* p1 , int* p2)
{
	int tmp = 0;
	tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}
int main()
{
	int a = 0;
	int b = 0;
	scanf("%d%d", &a, &b);
	printf("交换前,a = %d b = %d\n", a, b);
	swap(&a, &b);
	/*int p1 = &a;
	int p2 = &b;
	swap(p1, p2);*/
	printf("交换后,a = %d b = %d\n", a, b);
	return 0;
}

In the actual parameters, I passed the addresses of a and b into it, and the formal parameters used two pointers p1 and p2 to store the addresses of a and b, then used the * operator to find the addresses of a and b and modified them Content.

5. Nested call and chain access of functions 

5.1 Nested calls 

#include <stdio.h>
void new_line()
{
	printf("hehe\n");
}
void three_line()
{
	int i = 0;
	for (i = 0; i < 3; i++)
	{
		new_line();
	}
}
int main()
{
	three_line();
	return 0;
}
int main()
{
	void test()
	{
		//......
	}
	return 0;
}

 This is nested definition, that is, it is not allowed to define a function inside a function.

Functions can be called nestedly, but definitions cannot be nested.

5.2 Chain access

Use the return value of one function as an argument to another function.

Why does this print 4321? We have to check the return value of printf. So it's no surprise that the result is 4321.

6. Function declaration and definition

6.1 Function declaration:

 When the program is executed, it goes down step by step. The picture below is written in the textbook. When we call a function, there must be a function declaration, but if the function is written above the main function, there will be no more A statement is required.

 1. Tell the compiler what a function is called, what its parameters are, and what its return type is. But whether it exists or not, the function
    declaration cannot decide.
2. The declaration of the function generally appears before the use of the function. It must be declared before use.
3. The declaration of the function should generally be placed in the header file.

6.2 Function definition:

The definition of a function refers to the specific realization of the function, explaining the function realization of the function. 

6.3 Writing at work 

We will write the Add function as an addition module. Just call the header file of Add in the main function. Writing the code separately will make the logic clear.

 

 

 

So why separate source and header files? If you are a programmer, you only want others to use the modules you write. But you don't want others to see what you have achieved, just tell him how to use it. At this time, you can set the .c file as a static library. That is to say, the function declaration is given out, and how the function is realized is hidden.

Next, I will teach you how to operate. Let's first remove the above add.h add.c files from the project.

Then close the current solution, create a new add project, and then open the location of the previously closed solution file, and cut add.h add.c into the add file.

 

 

 If, unwilling to expose the code. Can be compiled into a static library.

 

 Open the add.lib file with Notepad, and you will find that it is all messed up. 

 Then put both the add.lib file and the add.h file into the file of our original solution 

 

Add add.h to test.c, #pragma comment(lib,"add.lib") means importing a static library. 

 

With that, we're done.  The advantage of this is that some core code can be hidden. 

In fact, you can use the library functions in the c language by directly including the header files, but in fact you can’t see how these library functions are implemented. The reason why they can be used directly is that the VS compiler defaults these libraries The static library of functions is imported.

7. Function recursion

7.1 What is recursion? 

The programming technique in which a program calls itself is called recursion. Recursion is widely used as an algorithm in programming languages. A process or function has a method of calling itself directly or indirectly in its definition or description. It usually converts a large and complex problem layer by layer into a smaller-scale problem similar to the original problem to solve. The recursive strategy is only A small number of programs can be used to describe the multiple repeated calculations required in the problem-solving process, which greatly reduces the amount of code in the program. The main way of thinking about recursion is: to make big things small

7.2 Two Necessary Conditions for Recursion 

There are constraints, and when this constraint is met, the recursion will not continue.
Getting closer and closer to this limit after each recursive call

I will explain some topics of recursion in detail in the next article, I hope everyone will like it!

Guess you like

Origin blog.csdn.net/m0_63562631/article/details/125502437