In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to use functions in C language". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to use functions in C language".
Function definition
Baidu encyclopedia's definition of function: subroutine
In computer science, a subroutine is a part of code in a large program, which consists of one or more statement blocks, which is responsible for accomplishing a specific task and is relatively independent compared with other codes.
Generally speaking, there are input parameters and return values that provide encapsulation of the process and hiding of details, and these codes are usually integrated into software libraries.
General format of function
C language function classification library function
In order to support portability and improve the efficiency of the program, the basic library of C language provides a series of library functions, which are the encapsulation of the code that implements a specific function. When users need to achieve this function, you only need to call this library function to facilitate programmers to develop software.
Classification of library functions
Commonly used library functions can be simply divided into: IO function, string operation function, character operation function, memory operation function, time / date function, mathematical function and so on.
Learning of library function
Here we mainly provide two websites to provide user learning library functions.
(http://www.cplusplus.com)
(http://en.cppreference.com)
Custom function
The custom function is the source code written by the programmer to realize the function according to the function to be completed.
Custom functions, like library functions, have function names, return value types, and function parameters.
Code example 1: write a function to find the maximum of two integers.
Int get_max (int x, int y) {return (x > y? X: y);} int main () {int a, b; scanf ("% d% d", & a, & b); int MAX = get_max (a, b); printf ("MAX =% d\ n", MAX); return 0;} 10 20MAX = 20 Please press any key to continue. . .
Code example 2: write a custom function to exchange two integers
Void swap (int * x, int * y) {int temp = * x; * x = * y; * y = temp;} int main () {int a, b; scanf ("% d% d", & a, & b); printf ("before exchange: a =% d b =% d\ n", a, b); swap (& a, & b) Printf ("after exchange: a =% d b =% d\ n", a, b); return 0;} 10 20 before exchange: a = 10 b = 20 after exchange: a = 20 b = 10 Please press any key to continue. . . The actual parameters of the function
The parameters actually passed to the function can be variables, constants, expressions, functions, no matter what type of arguments the arguments are, they must have definite values when making a function call, in order to pass these values to the formal parameters.
Formal parameter
Formal parameters refer to the variables in parentheses after the function name of the function being called. Formal parameters allocate memory space for storage only when the function is called, and when the function is not called, they do not occupy the storage unit in memory, so they are called formal parameters or virtual parameters. And the function call is automatically destroyed immediately after the function call is completed, so the parameter is only valid in the called function.
Shape participates in the connection of arguments: when a function is called, the argument is passed to the parameter, and the parameter is a temporary copy of the parameter, so modifications to the parameter will not affect the argument.
The actual parameter unit and the formal parameter unit are different units. After the call ends, the formal parameter unit is released, and after the function call returns to the main tone function, the formal parameter can no longer be used. The parameter unit still retains and maintains the original value. Therefore, when a called function is executed, if the value of the parameter changes, it does not change the value of the argument in the main function.
Function call
After defining the function, we need to call this function to execute the code snippet in the function. This is different from the main () function. Main () sets the main function to be called automatically for the compiler without human calls. We all call other functions in the main () function, and there is only one main () function in a C program.
Pass value call
The formal parameters and arguments of the function occupy different memory space, and the parameters are destroyed immediately with the end of the tuned function, so the modification of the parameters will not affect the parameters.
Code example:
Void swap (int x, int y) {int temp = x; x = y; y = temp;} int main () {int a, b; scanf ("% d% d", & a, & b); printf ("before exchange: a =% d b =% d\ n", a, b); swap (a, b) Printf ("after exchange: a =% d b =% d\ n", a, b); return 0;} 10 20 before exchange: a = 10 b = 20 after exchange: a = 10 b = 20 Please press any key to continue. . . Address call
Address call, as the name implies, is to pass the address to the called function, (actual participation parameter "different levels").
Code example:
Void swap (int * x, int * y) {int temp = * x; * x = * y; * y = temp;} int main () {int a, b; scanf ("% d% d", & a, & b); printf ("before exchange: a =% d b =% d\ n", a, b); swap (& a, & b) Printf ("after exchange: a =% d b =% d\ n", a, b); return 0;} 10 20 before exchange: a = 10 b = 20 after exchange: a = 20 b = 10 Please press any key to continue. . . No parameter function call
If you are calling a no-argument function, you cannot add "argument", but the parentheses cannot be omitted.
Void test () {} int main () {/ / function call test (); / / right, parenthesis () cannot omit test; / / error, function definition without parameter return 0;} function declaration and definition function declaration
The so-called function declaration is to tell the compiler that there will be or already have a corresponding function, what the parameter is and what the return type is, but the compiler is not sure whether it exists or not.
The declaration of a function usually occurs before the function is used, first declared and then used. Mainly used in multi-file programming, generally put in the header file.
Definition of function
The definition of a function refers to the code interpretation that specifically implements a function.
# include int get_max (int x, int y); / / function declaration, the semicolon cannot be omitted / / int get_max (int, int); / / another way int main () {int a = 10, b = 25, MAX = 0; MAX = get_max (a, b); / / function call printf ("MAX =% d\ n", MAX); return 0 The definition of function int max (int x, int y) {return x > y? The difference between function declaration and definition of x: y;}
Definition refers to the realization of the function, including the specified function name, function type, formal parameter and its type, function body and so on. It is a complete and independent function unit.
The function of the declaration is to inform the compilation system of the function name, function type, and the number, type, and order of formal parameters (note, excluding the function body), so that when compiling statements containing function calls, check them accordingly (for example, whether the function name is correct, whether the type and number of actual participating parameters are consistent).
Introduction of exit and return
Here we briefly introduce the differences and relations between return and exit:
Connection: in the main function, exit and return play the same role.
Difference: calling return in a subfunction only means that the subfunction terminates. If exit is called in a subfunction, then the program terminates.
The above is all the content of this article "how to use functions in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.