In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the basic use of C language functions and the declaration and definition of functions". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn the basic use of C language functions and the declaration and definition of functions.
Catalogue
Objectives of this chapter
What is the function?
Classification of functions in C language
Library function
How to learn to use library functions?
Custom function
Parameters of the function
Call to the function:
Nested calls and chained access to functions
Nested invocation
Chain access
Declaration and definition of function
Function recursion
What is recursion?
Two necessary conditions for Recursion
Recursion and iteration
Summary
Objectives of this chapter
Hello, bald men, let's learn functions together today!
Goal: this chapter mainly deals with the basic use and recursion of functions.
What is the function?
We often see the concept of function in mathematics. But do you know the functions in C language? Definition of function in Wikipedia: subroutine
In computer science, a subroutine (English: Subroutine, procedure, function, routine, method,subprogram, callable unit) is a part of code in a large program that consists of one or more statement blocks. It is responsible for accomplishing a specific task and is relatively independent compared with other codes.
There are generally input parameters and return values that provide encapsulation of the process and hiding of details. This code is usually integrated into a software library.
Classification of functions in C language
1. Library function
two。 Custom function
Library function
Why are there library functions?
1. We know that when we learn C programming, we can't wait to know the result after a code has been written and want to print the result to our screen. At this time, we will frequently use a function: print the information to the screen according to a certain format (printf).
two。 In the process of programming, we will frequently do some string copy work (strcpy).
3. In programming, we also calculate, always calculate the k power of n such operations (pow).
Like the basic functions we described above, they are not business code. In the process of development, every programmer may use it. In order to support portability and improve the efficiency of the program, the basic library of C language provides a series of similar library functions to facilitate programmers to develop software.
So how to learn library functions?
Let's take a simple look here: https://www.cplusplus.com
To sum up, the library functions commonly used in C language are:
IO function
String operation function
Character operation function
Memory operation function
Time / date function
Mathematical function
Other library functions
Note: but a secret that library functions must know is that if you use library functions, you must include the header file corresponding to # include.
How to learn to use library functions?
You don't need to remember all the library functions. You can https://www.cplusplus.com if you need to find them at this URL.
(so it's important to understand English. Of course, you can also choose translation software.)
Custom function
If library functions can do everything, what do you need programmers for?
So what is more important is the custom function.
Custom functions, like library functions, have function names, return value types, and function parameters. But the difference is that we make our own plans. 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 argument
Here's an example:
Write a function to find the maximum of two integers
# Design of include / / get_max function int get_max (int x, int y) {return (x > y)? (X): (y);} int main () {int num1 = 10int num2 = 20int max = get_max (num1, num2); printf ("max =% d\ n", max); return 0;}
Return (x > y)? (X): (y); the meaning of this statement is to determine whether X is greater than Y. if X > Y, select the X value on the left, otherwise select the Y value on the right.
Parameters of the function
Actual parameters (arguments):
The parameters actually passed to the function are called arguments. Arguments can be: constants, variables, expressions, functions, etc. No matter what type of quantity the arguments are, they must have certain values when making a function call in order to pass these values to the parameter.
Formal parameters (formal parameters):
Formal parameters refer to the variables in parentheses after the function name, because formal parameters are instantiated (allocating memory units) only when the function is called, so they are called formal parameters. Formal parameters are automatically destroyed when the function call is complete. Therefore, formal parameters are only valid in functions.
Write a function to exchange the contents of two integer variables
# include void Swap1 (int x, int y) {int tmp = 0 int tmp = x py x = y px = tmp;} void Swap2 (int * px, int * py) {int tmp = 0 py tmp = * px;*px = * py;*py = tmp;} int main () {int num1 = 1mitint num2 = 2py Swap1 (num1, num2); printf ("Swap1::num1 =% d num2 =% d\ n", num1, num2); Swap2 (& num1, & num2) Printf ("Swap2::num1 =% d num2 =% d\ n", num1, num2); return 0;}
The arguments in the above code Swap1 and in the Swap2 function, x _ line y _ dir p _ x _ py are formal arguments. The num1,num2 passed to Swap1 in the main function and the & num1,&num2 passed to the Swap2 function are actual arguments.
After analysis:
Here you can see that when the Swap1 function is called, XMagee y has its own space and has the same content as the argument. So we can simply think that the instantiation of the formal parameter is actually equivalent to a temporary copy of the actual parameter.
When the Swap2 function is called, the px,py does not open up any more space, but the address of the num1,num2.
Call to the function:
Pass value call
The formal parameters and arguments of the function occupy different memory blocks, and the modification of the parameters will not affect the parameters.
Address call
An addressing call is a way to call a function by passing the memory address of a variable created outside the function to the function parameter.
This way of passing parameters can establish a true relationship between the function and the variables outside the function, that is, the variables outside the function can be manipulated directly inside the function.
Nested calls and chained access to functions
Functions and functions can be combined organically.
Nested calls # include void new_line () {printf ("hehe\ n");} void three_line () {int iTuno; for (iTun0; I 9) {print (n / 10);} printf ("% d", n% 10);} int main () {int num = 1234; print (num); return 0;}
Writing functions does not allow you to create temporary variables and find the length of a string.
# include int Strlen (const char* str) {if (* str = ='\ 0') return 0; else return 1 + Strlen (str + 1);} int main () {char* p = "abcdef"; int len = Strlen (p); printf ("% d\ n", len); return 0;}
The above two codes are the use of function recursion.
Recursion and iteration
Find the factorial of n. (do not consider overflow)
Int factorial (int n) {if (n)
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.