C language [library function and custom function] detailed explanation

1. What is a function

We may not be unfamiliar with functions when we mention them. We may think of functions in mathematics, for example:
insert image description here
this is a function, but,Do you understand functions in C language?
Definition of function in Wikipedia:subroutine

  1. In computer science, a subroutine (English: Subroutine, procedure, function, routine, method, subprogram, callable unit) is a certain part of the code in a large program, consisting of one or more statement blocks. It is responsible for completing a specific task and is relatively independent from other codes.
  1. Generally, there will be input parameters and a return value , providing the encapsulation of the process and the hiding of details. These codes are often integrated as software libraries.

Functions in C language can be divided into two categories: library functions and custom functions:
Next, let's lead you to learn:

2. Library functions

1. What is a library function

Library function (Library function) is a way of encapsulating functions into a library for users to use.
the way isCompile some commonly used functions and put them in a file for different people to call
When calling, just add the file name where it is located with #include<>.

2. Why are there library functions?

We know that when we are learning C language programming, we can't wait to know the result after a code is written, and want to print the result to our screen to see. At this time, we will frequently use a function :
print the information to the screen in a certain format (printf).
In the process of programming, we will frequently do some copying of strings (strcpy).
When programming, we also perform mathematical calculations, such as calculating n to the kth power (pow)。

Like the basic functions we described above, they are not business codes. Every programmer may use it during the development process . In order to support portability and improve program efficiency, soA series of similar library functions are provided in the basic library of C language, which is convenient for programmers to develop software

A brief summary, the library functions commonly used in C language are:

IO functions
String manipulation functions
Character manipulation functions
Memory manipulation functions
Time/date functions
Mathematical functions
Other library functions

3. Precautions for using library functions

Printf, scanf, etc. that we often use are library functions in C language. A secret that must be known when using these library functions is:To use library functions, the header files corresponding to #include must be included.
If it is not included and used directly, the compiler may issue corresponding warnings. For example:
write a simple code:

int main()
{
    
    
	printf("hello \n");
	return 0;
}

We do not include the corresponding header file, run directly:
insert image description here
After we include the corresponding header file, run again:
insert image description here

4. Suggestions for learning library functions

After understanding the library functions, we might think, theseDo library functions need us to remember them all?
unnecessary

We just use them when we need them.

Recommend one hereQuery the website of the library function:
Link: After the link
is opened, this page looks like this:
insert image description here

For example, if we query a strcpy, there are detailed explanations and examples:

insert image description here
But it's in English, and English is very important. At least read the literature.
Of course, you can also use tools to translate.

Other tools for learning library functions

1. MSDN (Microsoft Developer Network)
2. http://en.cppreference.com (English version)
3. http://zh.cppreference.com (Chinese version)

3. Custom functions

Next we learn custom functions:
If library functions can do everything, what do programmers need to do?
So it's even more important that the custom function

1. What is a custom function

Like library functions, custom functions have function names, return value types, and function parameters.
But the difference is that theseIt's all designed by ourselves. This gives programmers a lot of room to play.
The composition of the function:

ret_type fun_name(para1, *)
{
    
    
	statement;
}

insert image description here
For the return type of a function:

1. If the return type of the function is written as void, it means that this function does not return any value and does not need to return.
2. If a function does not write the return type, it will return the int type by default, but this is not recommended

2. Examples of custom functions

Let me give an example of a custom function to help you get familiar with how to customize a function:

Write a function to find the largest of two integers.

#include <stdio.h>

int get_max(int x, int y)
{
    
    
	return x > y ? x : y;
}


int main()
{
    
    
	int num1 = 10;
	int num2 = 20;
	int max = get_max(num1, num2);
	printf("max = %d\n", max);
	return 0;
}

insert image description here
The above is the introduction to the C language library functions and custom functions. Welcome to correct me! ! !

Guess you like

Origin blog.csdn.net/m0_70980326/article/details/126101989