Continuing from the last time!
Last time we discussed break and continue, leaving two pieces of code! The result of the first piece: 01234 Explanation: When i++ reaches 5, break is executed, and the code exits the loop, ending! The result of the second piece: 12346789 Explanation: When i++ reaches 5, continue is executed, the code continues, but skips the subsequent code in the loop, meaning it does not execute printf(“%d”, i); and directly jumps to i++, at this point i becomes 6, thus the code result is 12346789
01Function Definition
Without further ado, let’s get straight to the point, today’s focus is on functions. As the name suggests, a function is the y that is ultimately required to solve a problem, so it can be understood as: it can be summarized into a problem, and then solved through a series of operations.
Now, given two numbers: 1, 2, how would you calculate the sum of these two numbers?
If the summation step is just a small part of a code? Suppose a project code needs to add these two numbers 100 times. Then functions come in handy, allowing you to write the addition step as a function.
02Code Comparison
Code 1: (Non-function)
#include<stdio.h>int main(){ int num1 = 0; int num2 = 0; scanf("%d %d", &num1, &num2); int sum = num1 + num2; printf("%d\n", sum); return 0;}
Code 2: (Function)
#include<stdio.h>int Add(int x, int y){ int z = 0; z = x + y; return z;}int main(){ int num1 = 0; int num2 = 0; scanf("%d %d", &num1, &num2); int sum = Add(num1, num2); printf("%d\n", sum); return 0;}
Compare these two pieces of code, a simple addition, why write it as a function, and it seems to have become more complex?
Because the next time you call the addition, it will be very convenient, as you can directly call the function!
03Function
So, what does a function look like, and how should it be written?
ret_type fun_name(para1,*)
{
statement;
}
ret_type: return type
fun_name: function name
para1: function parameters
Let’s explain the addition function:
int Add(int x, int y)
{
int z = 0;
z = x + y;
return z;
}
Here, Add is the function name fun_name, you can name it yourself, but it’s best to use a corresponding meaningful English name, so that others can refer to it easily.
(This brings up another issue, for various names you create in the code, it’s best to use meaningful English names, because in a large project, it’s not possible for one programmer to complete it alone, collaboration from various departments is needed, so others also need to refer to your code, try to use consistent English that everyone can understand.)
Here, int is the return type ret_type, related to z in {}, z’s type determines the return type. If z is a character, then the return type would be char…
Here, the int x, int y in () are the function parameters para1, acting as a medium to pass the actual parameters into the function for it to run, and these are called formal parameters, which can be understood as a form, placed here as a medium, the actual parameters will be introduced later. The int before x and y indicates their respective types, both are integers, so int x, int y.
All parts within {} are statements, containing the process and method of the function’s operation, that is, the steps to implement the function, which is the method of addition.
Now let’s practice, write a subtraction function! Boldly share your code!
Regarding functions, this is just the tip of the iceberg, today’s function is a user-defined function, meaning a function that you define yourself.
In the very beginning, many functionalities needed to be implemented frequently, so some experts thought, if I directly write these frequently needed functionalities as functions, I can just call them directly when needed, wouldn’t that be very convenient? This would double the efficiency, so these experts created a library called library functions, implementing many functionalities and placing them in the library, such as printing results on the screen (printf), copying strings (strcpy)… This explains why we need to include header files for some functions.
These library functions do not need to be memorized, but you should know how to look them up:http://en.cppreference.com, it’s best to read in English, so you can gradually improve.
For each function in the library functions, everyone can try to write it themselves and compete with the experts.
That’s all for today’s functions, I hope everyone has gained something! Thank you all.

