※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  > | advanced by | contents  | that returns in <    
                   < [modosusu] > Color magazine monochrome light and shade   font predetermined, Gothic Ming-style type longhand   size Konaka large   standard  


  Make the self-made function.   

  1. ..clause 1..: Making of program parts
  2. ..clause 2..: Make the self-made function.
  3. ..clause 3..: Prototype declaration
  4. ..clause 4..: Call the self-made function.

[1] Making of program parts

It has already almost explained a basic C language instruction here.
I am reminded of various programs that have been made so far.

By the way, when you want to use the program made once again
The first simple : though copies, pastes in the main function, and applies it.
It is mixed [gocchanina] when there is a large amount of programs.
In becoming the program not to understand division, the mistake is not found.

It is called as recycling to use the program made before again.
It is called as making to parts to recycle as a function.
Even if it is a huge program if you prepare a lot of parts
It is very efficient because it completes it only by combining the parts.


[   Recycling   ]
Save labor because the program made before is used.

[   Making to goods   ]
Make a small program with a single function.
Method of making big program from combination of it.
Here, the program that displays the total of 1-100 explains in clause 3 of Chapter 4 paragraph 2 before
Recycle easily as parts by making it to the function.

It returns to contents.


[2] Make the self-made function.

When the program that requests the total of 1-100 is not made a function to make it to parts, it doesn't become it.
However, it is not the first time to actually make the function for you.
Do you remember how to make the main function being explained at the time of beginning of the first?
After that, I do not think that I considered it because I made the main function as a matter of course.

How to make the self-made function is neither main function nor any kooky [wari].
The function of a new making by oneself can be made only from changing the name of the main function.
Of course, the processing of the contents can be freely described.

It is very easy if it understands here.
Change the name, and refill contents to the program that displays the total of 1-100.
The sum function newly made is as follows.

 
#include <stdio.h> 

int main(void)  
{
	return 0;  
}

int sum(void)  
{
	printf("%d\n",(1 + 100) * 100 / 2);  
	return 0;
}
The sum function was completed by this.
The program can be recycled as long as the usage of the sum function is understood now.

It returns to contents.


[3] Prototype declaration

Well, though it is a place where it wants to explain the usage of the sum function immediately
Actually, another one might have to be prepared ahead of that.

Though the sum function was made as newly ahead
Actually, the sum function cannot be used being possible this to hold.
The reason for the compiler is that it doesn't know the sum function is newly made.

According to circumstances, the function newly made in the compiler need not be given.
The reason for the compiler is that it automatically recognizes it if a new function is discovered.
However, it is only in the function discovered from the function back to be able to use a new function.

In a previous example, the main function was first made, and, next, the sum function was made.
In this case, the sum function is not discovered at the stage where the main function is analyzed.
It becomes impossible to use the sum function in the main function therefore.

One of the methods of solving this is a method of previously writing the sum function.
The following program is an example of previously writing the sum function.

 
#include <stdio.h> 

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

int main(void)  
{
	return 0;
}
The sum function can be used from the main function without trouble if it does in this manner.
Function..a lot..always..order..related to..worry.
It is troublesome to bring the first a necessary function.

If you make it to the list beforehand though it doesn't know what function made
Worry that separately writes the function ahead and writes is lost.
Arrange the shape of the function to make the list of the function.

What is the shape of the function of the function behind the first line;[Wotsuketamono], [nokotodesu].
Three (the function name, the type of the function, and the type of the argument) are written accurately.
The following program is an example of writing the outline of the sum function beforehand.

 
#include <stdio.h> 

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

int main(void) 
{
	return 0; 
}

int sum(void) 
{
	printf("%d\n",(1 + 100) * 100 / 2); 
	return 0; 
}
The outline of such a function is called a prototype declaration.
Even if the function is written in what order, it doesn't relate as long as the prototype declaration is done.

[   Prototype declaration   ]
Beforehand in declaring the shape of the function at the head
The function should be able to be used from all other functions.
Only, though the prototype declaration is not necessary for main function alone
All the prototype declarations are needed in other functions.

[ True colors of   # include   ]
Actually, the prototype declaration of the printf function etc. that have been used up to now :.
Every time, it was written in the stdio.h file that had been written at the head.
This is the maximum reason to write # include < stdio.h >.

It returns to contents.


[4] Call the self-made function.

Even if it is a self-made function, the usage is quite same as the current function.

Apply () for the printf function after printf.
The character string and the variable to want to display in that were called specifying it.
The sum function can be called as much as this.

However, the argument : for the sum function There is no passed information because it is void.
Specify only () because () cannot be omitted even in this case.
The following program is an example of calling the sum function from the main function.

 
#include <stdio.h> 

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

int main(void) 
{
	sum(); /* call part */  
	Return 0;
}

int sum(void) 
{
	printf("%d\n",(1 + 100) * 100 / 2); 
	return 0; 
}
The execution result of this program becomes as follows.

5050
It sequentially explains the flow of this program.
1 and the main function are called.
Sum() in 2 and the main function is executed, and it jumps to the sum function.
The printf function in 3 and the sum function is executed.
Return in 4 and the sum function is executed, and it returns just behind sum() in former main function.
Return in 5 and the main function is executed, and the program ends.
Importance here are to be done all concrete processing in the sum function.
It can be left to the sum function to calculate and to display the total of 1-100.
Call it by this making to parts when you like sum function.
It came to be able to display the result of the total of 1-100 on the screen at any time.

It returns to contents.


< - It is advanced -> | in | head that returns  to returning  next |.