So far, we've covered most of the basic commands in C.
Memories of the various programs I've created come to mind.
By the way, if you want to use a program you've already created,
The simplest approach is to copy and paste it into the main function.
When there are a large number of programs, they can become jumbled and confusing.
It will undoubtedly become an incomprehensible program.
We call reusing a previously created program [R]reuse[/R].
We call breaking something down into reusable components modularization.
If you have enough components, even a massive program can...
It's very efficient because it's completed simply by assembling the parts.
キーワード
【Recycling】
Saving effort by utilizing previously created programs.
キーワード
【Modularization】
単独の機能を持つ小さなプログラムを作り、 それを組み合わせることで大きなプログラムを作る方法。
Here, we present a program that displays the sum from 1 to 100, as explained in Chapter 4, Section 2, item 3.
Functioning in this way makes them easily reusable as components.
Creating custom functions
To modularize, you need to make a program to calculate the sum from 1 to 100 into a function.
However, the truth is, creating a function isn't actually new to you.
Do you remember when I explained how to create a main function first?
I suppose I hadn't realized it because I'd been dutifully creating a main function ever since.
Creating your own functions is no different from creating a main function.
Simply renaming the main function allows you to create a new custom function.
Of course, you are free to describe how to process the contents as well.
Now that you understand this far, it's very simple.
It's just a matter of renaming it and putting it into a program that displays the sum of 1 to 100.
The newly created sum function is as follows:
That completes the sum function.
Once you understand how to use the sum function, you'll be able to reuse the program.
Prototype Declaration
Well, I'd like to explain how to use the SUM function right away, but...
Actually, there's one more thing we need to prepare beforehand.
I just finished creating a new sum function.
Actually, with just this, we can't use the sum function.
"This is because the compiler doesn't know that a new sum function has been created."
Sometimes, you don't need to tell the compiler about newly created functions.
Because the compiler will automatically recognize it if it discovers a new function.
However, a new function can only be used within functions discovered after it.
In the previous example, we first created the main function and then created the sum function.
In this case, when parsing the main function, the sum function has not yet been discovered.
Therefore, you will no longer be able to use the sum function within the main function.
One way to solve this is to write the sum function first.
The following program is an example of a sum function written earlier.
This way, you can use the sum function without any problems from the main function.
However, when there are many functions, it's always necessary to be mindful of the order of execution.
It's a hassle to bring the necessary functions upfront.
If you could have a list beforehand of what functions are being created,
You no longer need to worry about placing functions before or after.
To create a list of functions, you should list the form of the functions at the beginning of your program.
The shape of a function is what you get when you add a semicolon after the first line of the function.
"To be precise, you need to write the function name, the function type, and the argument types."
The following program is an example with a pre-written outline of the sum function.
Source code
#include <stdio.h>
/* Prototype Declaration */
int sum(void);
int main(void)
{
return 0;
}
int sum(void)
{
printf("%d\n", (1 + 100) * 100 / 2);
return 0;
}
I refer to this kind of function outline as a prototype declaration.
As long as you declare the prototypes, the order in which you write the functions doesn't matter.
Even custom functions are used exactly like existing ones.
In the case of the printf function, you need to put parentheses after printf.
I called it, specifying the strings or variables I wanted to display.
The sum function can be called in the same way.
However, in the case of the sum function, the argument is void, so there's no information to pass.
Even in this case, we cannot omit (), so we will specify () only.
The following program is an example of calling the sum function from the main function.
The results of this program's execution are as follows:
Execution results
5050
I will explain the flow of this program step by step.
Program flow
1. The main function is called. 2. The `sum()` function within the `main` function is executed, and execution jumps to the `sum` function. 3. The printf function inside the sum function is executed. 4. The return statement within the sum function is executed, and control returns to immediately after the sum() call within the original main function. 5. The return statement within the main function is executed, and the program terminates.
The key here is that all the specific processing is being done within the sum function.
You can leave the task of calculating and displaying the sum from 1 to 100 to the sum function.
This modularization allows you to call the sum function whenever you need it.
You can now display the sum of numbers from 1 to 100 on the screen at any time.
About This Site
Learning C language through suffering (Kushi C) is
This is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.