MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us

Automatic version identification


It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

C language learned by suffering
C language learned by suffering

Create your own functions

Program Componentization
Up to this point, we have already explained most of the basic instructions of the C language.
You will be reminded of the various programs you have created so far.

By the way, if you have created a program once and want to use it again, you can use the
The simplest way is to copy and paste it into the main function.
If you have a large number of programs, they will be jumbled together.
It is certain that the program will be incomprehensible.

Reusing a previously created program is called reuse.
Reuse as a function is called componentization.
If you have a lot of components, even a huge program can be
It is very efficient because it can be completed by simply combining the parts.


To save time and effort by using a previously created program.


Create a small program with a stand-alone function and
A method of creating a large program by combining them.

In this section, we will use the program previously described in Chapter 4, Section 2, Paragraph 3, to display totals from 1 to 100.
function so that it can be easily reused as a component.
Create your own functions
To make it a component, the program must be a function to find the sum of 1-100.
But actually, creating the function itself is not new to you.
Remember how we explained how to create the main function at the very beginning?
Since then, we have been creating the main function as a matter of course, and I don't think we were aware of it.

The way to create your own functions is no different from that of the main function.
Just by changing the name of the main function, you can create a new self-made function.
Of course, you can freely describe the process inside.

Once you know this much, it is very simple.
Just change the name and repackage the contents into a program that displays a total of 1-100.
The newly created sum function will look like this

Source Code
 #include <stdio.h>

int main(void)
    return 0;

int sum(void)
    printf("%d\n", (1 + 100) * 100 / 2);
    return 0;

The sum function is now complete.
Now all that is left is to figure out how to use the sum function, and the program can be reused.
prototype declaration
Now, I would like to explain how to use the sum function.
Actually, there is one more thing we need to prepare before that.

I just created a new sum function.
Actually, this is not the only way to use the sum function.
This is because the compiler does not know that a new sum function has been created.

In some cases, it is not necessary to tell the compiler about the newly created function.
This is because the compiler automatically recognizes new functions as soon as it discovers them.
However, the new function can only be used in functions discovered later than that function.

In the previous example, the main function was created first, then the sum function.
In this case, the sum function has not been discovered at the stage of analyzing the main function, and the
Therefore, it is no longer possible to use the sum function in the main function.

One solution to this is to write the sum function first.
The following program is an example of writing the sum function first.

source code
 #include <stdio.h>

int sum(void)
    printf("%d\n", (1 + 100) * 100 / 2);
    return 0;

int main(void)
    return 0;

In this way, the sum function can be used from the main function without problems.
However, if you have a lot of functions, you should always pay attention to the order relationship and use
It is tedious to bring the necessary functions to the top.

If you can list ahead of time what functions are being created, you can use the
You don't have to worry about writing a function before or after each one.
To create a list of functions, arrange the function forms at the beginning of the program.

A function form is the first line of a function followed by a ;.
To be precise, you should write the function name, the function type, and the argument type.
The following program is an example of a pre-written summary 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;

Such a function outline is called a prototype declaration.
As long as the prototype declaration is made, it does not matter what order the functions are written.

Prototype Declaration

By declaring the form of the function at the beginning in advance
To make that function available to all other functions.

The only function that does not require a prototype declaration is the main function.
All other functions require a prototype declaration.

#include identity
In fact, the prototype declarations we've been using, such as the printf function
It was written in the stdio.h file, which was written at the top of every file.
This is the main reason for writing #include.

Calling your own functions
Even if it is a homemade function, its usage is exactly the same as that of the previous functions.

In the case of the printf function, add a () after printf and
We called it by specifying the string or variable we wanted to display in it.
The sum function can be called in the same way.

However, in the case of the sum function, the argument is void, so there is no information to pass.
In this case, the () cannot be omitted, so only the () is specified.
The following program is an example of calling the sum function from the main function.

source code
 #include <stdio.h>

int sum(void); /* prototype declaration */

int main(void)
    sum(); /* calling part */
    return 0;

int sum(void)
    printf("%d\n", (1 + 100) * 100 / 2);
    return 0;

The result of executing this program will be as follows

Execution Result

The flow of this program is described in turn.

Program Flow
1. The main function is called.
Sum() in the main function is executed and jumps to the sum function.
The printf function in the sum function is executed.
4、return in the sum function is executed and returns immediately after sum() in the original main function.
5, return in the main function is executed, and the program ends.

It is important to note that all the specific processing is done within the sum function.
The sum function can be left to calculate and display totals from 1 to 100.
This componentization allows you to call the sum function whenever you want, and to use the
The result of the sum of 1-100 can now be displayed on the screen at any time.

About this Site

The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or better than any book on the market.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20


Open the 💬 comment submission box