In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to define and use functions in C language". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to define and use functions in C language.
About function
The function is the basic unit of C language, and the function contains the code to realize the function of the program.
The entrance of the C language program is located in the main () function. In order to facilitate the organization and cooperation, debugging and running, the main function is generally used as the main tone function to realize the operation of the program by calling other functions.
Their relationship is as follows:
1. The defined form of a function
First, I'll show you the complete function code format, and I'll have a general impression first.
Int add (int b); / / this is a function declaration that tells the system that there is such a function / / Note: if the function is written before the main function, then this code can be omitted from int add (int a minute int b) {int centering int b; return c;}
Function header
Format:
Returns the name of the type function (parameter 1, parameter 2, … )
For the above function example int add (int aint b), the int before add is the return type, add is the function name, int an is the first argument, and int b is the second argument.
A function named add is defined. After two integer data are passed into the function, the function will return the shaping data processed inside the function.
Function body
The function name is followed by everything in parentheses. The body of the function includes code that declares variables and performs functions of the function.
Explanation: the integer variable c is declared here, and the added value of parameter an and parameter b is accepted with c, and then the add function returns the value of c.
two。 Declaration of function
The declaration of the function tells the compiler what the function is called, what the arguments are, and what the return type is. But the specific content still depends on the functional code inside the function.
Form:
Returns the type function name (parameter)
Note that there is a ";" to end the statement.
3. Return statement
In C language learning, you will often see such a code return 0; this is also the last line of the main function, its function has the following two points:
Immediately exit from the current function and return to the program that dispatched the function.
Send the return value of the function to the program that calls the function later.
Let me show you an example:
/ / int Add5 (int n); / / the function declaration here can omit the code in the above line int Add5 (int n) {return n + 5; / / see if the statement after return can be executed printf ("squeak if it can be executed");} int main () {int a = 1 Add5 (b); printf ("% d", b) Return 0;}
The code behind return will not run.
4. Function parameter
Parameters can be divided into formal parameters and actual parameters according to the way they are transmitted.
4.1 formal parameters (value call)
The function of the formal parameter is to transfer the value of the parameter to the function, but the value of the parameter itself will not be modified by the function. The previous examples are all formal parameters (abbreviations of formal parameters).
Let me show you an example:
The void fuc (int n); / / void type means that the return type is empty and there is no return value, but the internal code is still run. / / the statement here cannot be saved! The function is defined under the main function. Int main () {int a = 10; fuc (a); / / passed the value of a to n printf ("% d", a); return 0;} void fuc (int n) / / here is the definition of the function {n = 0;}
As a result, the value of print an is 10, which is not modified to 0 by fuc ().
4.2 actual parameters (addressing calls)
The actual parameter is by passing the address of the parameter to the inside of the function, and the function directly modifies the value of the actual variable through the address. Therefore, the actual parameter can be modified by the function.
Take a look at the effect of passing arguments:
Void fuc (int* n); / / the type of change 1n is a pointer, which stores the address of the actual variable int main () {int a = 10; fuc (& a); / / change two & to get the address of the variable directly, where the address of an is passed to the function internal printf ("% d", a); return 0 } void fuc (int* n) / / change three is actually the same as change one / / the variable here is nMaginn, the type is int*,n, and now the address of variable an is {* n = 0;}
For * n = 0
* in order to dereference operator, you can take out the value stored in the address. Here, the number corresponding to the address stored in n is assigned to 0.
The running result is as follows, and the value of an is changed to 0
4.3 No parameters
Most of the time, the function does not need parameters, and the operation of the function will not be affected if there are no parameters. Generally, there is no need to set parameters when printing a menu. Here is a casual demonstration:
Void Member_Menu (); void Member_Menu () {printf ("*\ n"); printf ("user Management: > >\ nPlease enter the operation you want to do:\ N1. Member addition\ N2. Member deletion\ n3. Member modification\ n");}
The function of the runtime function is to print the menu.
5. Function calls 5.1 nested calls
Other functions can be called inside the function. Such as:
Void fuc1 () {printf ("function 1");} void fuc2 () {printf ("function 2");} void fuc3 () {fuc1 (); fuc2 ();} int main () {fuc3 (); return 0;}
5.2 function recursion
To put it simply, recursion is to call yourself, which is very ingenious. It can use a small number of programs to describe the repeated calculations needed in the problem-solving process, which greatly reduces the amount of code of the program.
The key points of recursive implementation:
Find the recursive formula
Set the end flag
Now let's show you the use of recursion:
Fibonacci series: F (0) = 1 Magi F (1) = 1 Magi F (n) = F (nmur1) + F (nmuri 2) (n > = 2)
Recursive version:
Int fib (int n) {if (n 1) {for (k = 1, I = 1; k < n; KQ +) {j = I * So far, I believe you have a better understanding of "how to define and use functions in C language". You might as well do some practical work! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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: 272
*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.