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


  Variable that longevity is exhausted in function   

  1. ..clause 1..: Longevity of local variable
  2. ..clause 2..: The same name is another variable.
  3. ..clause 3..: Independence of function
  4. ..clause 4..: In block accurate

[1] Longevity of local variable

It did not explain the relation between the function and the variable though it explained the function in the preceding chapter.
Here, the count function is made, and it explains the relation between the function and the variable based on it.
The count function is a function that counts the called frequency.
Especially, call for convenience' sake like that though it is not because there is such a term.

It can be achieved considerably easily if it thinks simply.
Only have to prepare the variable, and, every time, to do to it by one.
The following program is an example of achieving the above-mentioned idea as it is.

 
#include <stdio.h> 

int countfunc(void);  

int main(void)  
{
	countfunc();  
	countfunc();  
	countfunc();  
	return 0;  
}

int countfunc(void)  
{
	int count = 0; /* initialization */ Count++;  
	printf("%d\n",count);  
	return count;
}
The execution result of this program becomes as follows.

1
1
1
The answer is all one though it is necessary to have called the function that adds one to variable count three times.
It might be a cause to substitute 0 in the part of initialization possibly.

Then, I want to use it without initializing the variable daring to disregard a warning.
In this case, though it doesn't understand what figure comes out in the beginning
Still, the third times of one than last time a lot or more are to be sure to go out secondarily a lot in the call of the second times.
The following program is an example of not initializing variable count.

 
#include <stdio.h> 

int countfunc(void);  

int main(void)  
{
	countfunc();  
	countfunc();  
	countfunc();  
	return 0;  
}

int countfunc(void)  
{
	int count; /* There is no initialization (Do not do in reality) */ Count++;.   
	printf("%d\n",count);  
	return count;
}
The execution result of this program (In author's environment) was as follows.

5369
5369
5369
Both the 2nd and 3rd figures have become the same deals completely why.
Apparently, the result of doing by one seems not to be reflected at all.

The longevity of the variable is deeply related to this.
Actually, when the function ends, the variable declared in the function is thrown away.
Try to make the variable again when the same function is called again.
At this time, the value changes because the memory besides last time is used, too.

When you execute it in author's environment in case of ahead though the same value seems to have come out by chance
As for this, whether it drinks and it is neither [jo] completely accidental discovery nor the same value comes out after all are understood.

In a word, the longevity of the variable declared in the function is only in the function.
The variable in which longevity ends in the function is called a local variable in this manner.
Moreover, it might be called scope bringing longevity and the useful range etc. of the variable together.


[   Local variable   ]
Variable declared in function. The variable of the dummy argument is also similar.
When the function ends, it is thrown away.
When the function is called again, it is newly made.
Moreover, it is not possible to use it from other functions.

[   Scope   ]
Generic name of mechanism that range where identifier is effective is limited.
In general, the mechanism that the longevity of the variable is decided the useful range.

[2] The same name is another variable.

If the local variable is annulled when the function ends
It is sure to survive until the main function ends if it declares in the main function.
In the following program, the main function is an example of having declared the count variable.

 
#include <stdio.h> 

int countfunc(void);  

int main(void)  
{
	int count = 0;  
	countfunc();  
	countfunc();  
	countfunc();  
	return 0;  
}

int countfunc(void)  
{
	int count; /* There is no initialization (Do not do in reality) */ Count++;.   
	printf("%d\n",count);  
	return count;
}
The execution result of this program (In author's environment) was as follows.

5369
5369
5369
The result doesn't change into the main function though the count variable was declared either.
Moreover, though the count variable is adjusted to 0 by the main function
When thinking from the display of another value in the countfunc function completely
Apparently, it seems not to be able to use the variable declared in the main function by other functions.

The scope of a variable is deeply related to this.
It is only in the declared function that the local variable is effective.
Even if the variable of the same name is declared in another function, it becomes another variable.
Therefore, there is no influence in the variable of other functions even if the value of the variable in the main function is changed.

It returns to contents.


[3] Independence of function

I see ..making.. in each function even of here. quite separate the variable
When you say whether it is such a mechanism why
The purpose of the reason is to improve the independence of the function.

Though the sum function from which the total of min-max was requested was made in the preceding chapter
This function can be used at once even because of other programs.
To use the function easily, each function should be independent in this manner.

For instance, the num variable was used in the sum function.
If, when it is made to share the variable by all functions
When the num variable is used by other functions, the value of the variable is changed.
Therefore, when you use the sum function by other programs
It is necessary to examine whether there is variable of the same a name as prior all.

However, if the variable used by each function is independent
There is no worry that influences the variable of other functions at all.
It is opened from fear that the value of the variable is suddenly changed.


[   Classic language   ]
This mechanism is not in a classic language (Include HSP).
Therefore, so that the value of the variable is not changed without permission somewhere
It is necessary to brush off attention to the entire program.
Therefore, large-scale development is very difficult.

It returns to contents.


[4] In block accurate

It is accurately in the block though it has explained the longevity of the local variable up to now in the function.
The range where ..block.. [de] is inclosed is pointed at.
It is also possible to make the block as intentionally as the following program in the function.

 
#include <stdio.h> 

int main(void)  
{
	int value1 = 10;  
	int value2 = 20;  
	printf("1:value1 %d\n",value1);  
	printf("1:value2 %d\n",value2);  
	{
		int value1;  
		value1 = 30;  
		value2 = 40;  
		printf("2:value1 %d\n",value1);  
		printf("2:value2 %d\n",value2);  
	}
	printf("3:value1 %d\n",value1);  
	printf("3:value2 %d\n",value2);  
	
	return 0;
}
The execution result of this program becomes as follows.

1:value1 10
1:value2 20
2:value1 30
2:value2 40
3:value1 10
3:value2 40
There are two noteworthy parts.
One can declare value1 again in the second block intentionally made.
Moreover, it has been treated as another variable.
It understands from ten it when getting out from the block to the second times though value1 was 30.

Another one : in the block intentionally made Value2 can be used.
As for value2, the result changed while blocking it remains in the third display.

Though the same thing is possible because it is a block
It becomes confusing when there are a lot of variables of the same name as one function.

It returns to contents.


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