Home>Article>Backend Development> C language custom function (detailed graphic and text explanation)
Every C program has at least one function, that is, the main function main(), If the task of the program is relatively simple, all the code is written in the main() function. However, in actual development, the task of the program is often more complex. If all the code is written in the main() function, the main() function It will be very large and bloated, with complex structure and repeated code.
We can divide the code into different custom functions according to the logic of the program and the division of tasks. main() is more concerned with business logic and processing flow, when specific tasks need to be performed. , just call these custom functions.
In order to facilitate understanding, we divide functions into two types: one is a library function and the other is a custom function.
Library functions are provided by the C language or the system. They implement certain basic functions, such as scanf and printf, and can be used directly in the program.
Custom functions are functions written by programmers to complete a certain task. The purpose is to realize a certain function or to make the main program more concise. Programmers must declare and define custom functions before using them.
The custom function declaration tells the compiler the name of the function and how to call the function, including the data of the function return value Type, function name, parameter list.
Some programmers also refer to function declarations as function prototypes, but the wording is different, but the meaning is the same.
The syntax of declaring a function in C language is as follows:
Return value data Type return_type:After the function completes the task, it can return a value. return_type is the data type of the value returned by the function, which can be int, char, double or other customized data types. Some functions only perform tasks without returning a value. In this case, return_type is represented by the keyword void.
The declaration syntax of a function without a return value is as follows:
Function name function_name:The function name is an identifier, and the naming rules are the same as those of variable names.
Parameter list of function parameter list:When the function is called, the caller needs to pass values to the parameters of the function. The parameter list includes the type and order of parameters. The parameter list is optional, that is, the function can have no parameters.
The declaration syntax of a function without parameters is as follows:
Example of function declaration:
declares a function, the return value is int type, the function name is checksc, the function has only one parameter int height, note that the function The semicolon at the end of the declaration statement cannot be missing.
If you compare a custom function to a tool, the function declaration is the design drawing of the tool.
Custom functions are tools. If you want this tool to be used, it is not enough to just have design drawings. You must make the tool. , the definition of a function is the entity of this tool, providing the actual body of the function. In order to realize the function of the function, the programmer writes code for the required function.
The syntax of function definition in C language is as follows:
The return_type, function_name and function definition The parameter list must be consistent with the function declaration.
The function body contains the collection of statements that need to be executed in order to complete the task, placed within curly braces.
Example:
Note, do not add points after the function definition No., the following is wrong, and beginners are prone to making this mistake.
If the custom function is only in the caller program Used in the caller program, it can be declared and defined in the caller program. The declaration is generally in the upper part of the caller program, and the definition is generally in the lower part of the caller program. This is not a requirement of the C language, but to make the program easier to read. Programmers Agreed writing style.
Example of declaration and definition of custom functions in the caller program (book45.c):
##In book45.c, the function checksc, which determines the figure of a super girl, is not a public function and is only used by the draft program, so it is declared and defined in book45.c.
If the custom function is a public function module, such as comparing the size of two numbers, it can be declared in other header files (such as _public.h), and the caller The program needs to use the #include preprocessing directive to include the header file declared by the function.
If the custom function is a public function module, it can be defined in other program files (such as _public.c). When compiling, the caller program and The program files for custom functions are compiled together.
In the following example, the function max is declared in the _public.h header file, defined in the _public.c program file, and called in the book46.c program.
Header file of custom function (_public.h):
Program file of custom function (_public.c):
Caller program file (book46. c)
The compilation command is as follows:
Note:
1)In line 6 of book46.c
,
The header files included here are enclosed in double quotes, not angle brackets . Generally speaking, #include is used to include header files of library functions provided by C language, and #include "" is used to include programmer-defined header files.
2)When compiling the caller program, compile the caller program and the program file of the custom function together. Otherwise, the following error will occur during compilation.
The correct compilation instruction should be gcc -o book46 book46.c _public.c
3)There are no regulations on the naming of the header files _public.h and _public.c of custom functions. They are named by the programmer themselves, just like the naming of variables. In order to increase the readability of the program, use them as much as possible. Some meaningful names.
C language provides some declared and defined functions. Calling these functions can complete some basic functions. We put These functions are called C standard library (C standard library), or library functions for short, such as printf, scanf, memset, strcpy, etc. There are hundreds of library functions in the CC language, but only two to three tenths of them are commonly used. There is no need to remember them. In the following chapters, I will introduce the use of some common library functions in detail. The more you use them, the more you will become familiar with them.
The declared header file of the C language standard library function is stored in the /usr/include directory, as follows:
C language library function The definition file is /usr/lib/gcc/x86_64-redhat-linux/4.4.4/libgcc.a (different compiler versions have different directory names). This is a packaged static link library file. Static We will introduce the knowledge of link libraries later.
We did not include libgcc.a when we compiled the program with gcc. That is because the gcc compiler will include it by default. However, the header of the programmer-defined function Files and program files do not have this treatment and need to be included manually.
When we use a library function, if its header file is not included, a warning will appear during compilation. , as follows:
What header files should be included? There are two methods. One is to search for information on Baidu, and the other is to use the help provided by the Linux system. Taking the strcpy function as an example, enter man strcpy and press Enter at the command line, as follows:
man displays the function's declaration header file (line 5), function parameters, usage and return values.
Note that if the library function used in the program does not include its header file, it may not necessarily be a warning during compilation, but may also be an error, the function cannot be recognized, etc. This depends on Compilers, compilers for different C languages are different.
Library functions are declared and defined by the system and are ready-made tools. Custom functions are declared and defined by programmers themselves, and are tools supplemented by programmers. Whether they are off-the-shelf tools or supplementary tools, they are all tools, and they are the same to users, with no difference.
In C language, function calls are very flexible. They can occupy a line of statements, be assigned to variables as constants, or be used as parameters of functions.
If the return value of the function is void, it means that the task of the function is mainly to complete a certain function. It is usually written in a single line and has an exclusive statement.
如果函数的返回值不是void,如果单行书写,表示不关心它的返回值,也可以当成常量用于任何表达式中。
例如:
当程序调用函数时,程序控制权会转移给被调用的函数。被调用的函数执行函数体的代码,当函数的返回语句被执行时,或到达函数的结束括号时,就把程序控制权交还给调用者程序。
示例(book47.c)
运行结果
关于函数的参数,在很多教程中有很多说法,如“形参”、“实参”、“值传递”、“地址传递”、“引用”等,这些说法把简单的概念复杂化了,大家不必理会。
函数的参数可以理解为函数的局部变量,参数传递就是调用者程序把变量(或常量)的值复制一份给函数的参数,简单说来就是复制的过程。一旦完成数据的复制,调用者程序的变量和函数的参数就再也没有瓜葛了,所以,在函数调用的过程中,函数的参数的值发生改变并不会影响调用者程序的变量的值。
我们在调用函数的时候,不关心函数参数的名称,函数参数的名称是函数自己的事情,只在函数内部使用,与调用者无关。
示例(book48.c)
运行结果
const 是 constant 的缩写,意思是“恒定不变的”!它是定义只读变量的关键字,或者说 const 是定义常变量的关键字。用 const 定义常变量的方法很简单,就在通常定义变量时前面加 const 即可,如:
const int a = 10;
那么用 const 修饰后和未修饰前有什么区别呢?它们不都等于 10 吗?
用 const 定义的变量的值是不允许改变的,即不允许给它重新赋值,即使是赋相同的值也不可以。所以说它定义的是只读变量。这也就意味着必须在定义的时候就给它赋初值。
在变量前加const约束,主要用于定义函数的参数,表示该参数在函数中是只读取,不允许改变,如果函数中试图改变它的值,编译的时候就会报错。
例如:
函数max在声明的时候,参数a和b都加了const约束,在函数中,a=10;和b=20;这两代码试图改变a和b的值。编译的时候如下:
编译无法通过。
在我的课程中,会介绍常用的库函数,但也有漏掉的。查资料是程序员的基本技能。
例如要查找C语言复制字符串功能的函数,在百度上输入“C语言复制字符串”搜索就可以了,你想查什么就输入什么。
然后,打开多几个网页看看,基本上就差不多了。
注意一个问题,网上的内容太多太杂乱,重点关注百度百科的文章。
1)测试函数的功能
要使用函数,只需要关注它需要包含的头文件、函数名、返回值、参数的数据类型和参数的顺序,不必关心参数的名称。
以strcpy函数为例,函数的声明如下。
char *strcpy(char *dest, const char *src);
函数名:strcpy
返回值:返回dest,一般没用,不理它。
第一个参数是目标字符串;第二个参数是源字符串,有const约束。
写代码测试一下:
char strname[50]; strcpy(strname,"西施"); printf("=%s=\n",strname);
然后,看看输出的结果是不是“=西施=”,用=号夹住输出的内容是为了看清楚有没有其它的字符。
感谢大家的阅读,希望大家收益多多。
本文转自:https://blog.csdn.net/wucz122140729/article/details/98369860
推荐教程:《C语言》
The above is the detailed content of C language custom function (detailed graphic and text explanation). For more information, please follow other related articles on the PHP Chinese website!