The " variable " trap of Fortran function calls
Recently, the author forced a wave of "Fortran" programming under the pressure of taking the test. During the programming process, I found some traps that caught beginners off guard, and I will share them here.
I fortran functional programming is different from C/C++, Java and several points:
Here are a few key points to note about Fortran programming: 1. When calling a function with parameters, you must declare the type of the parameter, which is necessary 2. When using loop variables for loop processing, the loop variable must be declared as integer type 3. The function name represents the return value of the function body of the function. Whether it is in the body of the called function or the body of the calling program, it must be declared as For the return value variable named by the function name (that is, declaring the return value type of the function), the called function is modified with the keyword external in the calling program The format of the declaration is as follows: real fun external fun
II When calling a fortran function, we must declare the type of the parameter of the function in the parameter body before it can be used. Unlike C/C++ and Java, which are directly declared in the parameter parentheses, there is a more significant problem, which is declared in the Fortran function. And assign the value of the disposed variable in successive calls of the calling function will not be reinitialized with another call, see example:
! paractise.f90 ! ! FUNCTIONS: ! paractise - Entry point of console application. ! ! Example of displaying 'Hello World' at execution time. ! !**************************************************************************** ! ! PROGRAM: paractise ! ! PURPOSE: Entry point for 'Hello World' sample console application. ! !**************************************************************************** program paractise implicit none integer days, times, i real, allocatable ::averTemp(:) real fun external fun print *, "Please enter the number of days:" read *, days print *, "Please enter the number of periods per day:" read *, times allocate(averTemp(days)) do i = 1, days averTemp(i) = fun(times) end do print*, "Average temperature for each period of the day:" do i = 1, days print *, averTemp(i) end do stop end program paractise function fun(times) implicit none integer i, times ! declares the type of the function parameter, this is necessary real fun ! declares the return type of the function, this is necessary too real, allocatable ::timesinfo(:) real ::sum = 0 ! Follow here! ! ! ! ! print *, "Output the value of sum:", sum allocate(timesinfo(times)) print *, "Please input", times, "Temperature at times:" read *, timesinfo do i = 1, times sum = sum + timesinfo (s) end do fun = sum / times ! sum = 0 ! It should be noted here that, unlike languages like C and Java, end function fun ! The same function is called in the loop body, which is defined in the function body The variables of ! are shared during multiple calls, so when the body is accumulated, the sum needs to be cleared each time.
Test run screenshot:
According to the programming logic of languages similar to Java, this should not be the case! !
So, I tried to separate the declaration of the variable sum from the initial value, i.e.
real sum
sum = 0
There are program modifications as follows:
! paractise.f90 ! ! FUNCTIONS: ! paractise - Entry point of console application. ! ! Example of displaying 'Hello World' at execution time. ! !**************************************************************************** ! ! PROGRAM: paractise ! ! PURPOSE: Entry point for 'Hello World' sample console application. ! !**************************************************************************** program paractise implicit none integer days, times, i real, allocatable ::averTemp(:) real fun external fun print *, "Please enter the number of days:" read *, days print *, "Please enter the number of periods per day:" read *, times allocate(averTemp(days)) do i = 1, days averTemp(i) = fun(times) end do print*, "Average temperature for each period of the day:" do i = 1, days print *, averTemp(i) end do stop end program paractise function fun(times) implicit none integer i, times ! declares the type of the function parameter, this is necessary real fun ! declares the return type of the function, this is necessary too real, allocatable ::timesinfo(:) real ::sum ! Follow here! ! ! ! ! sum = 0 !sum type declaration separate from assignment print *, "Output the value of sum:", sum allocate(timesinfo(times)) print *, "Please input", times, "Temperature at times:" read *, timesinfo do i = 1, times sum = sum + timesinfo (s) end do fun = sum / times ! sum = 0 ! It should be noted here that, unlike languages like C and Java, end function fun ! The same function is called in the loop body, which is defined in the function body The variables of ! are shared during multiple calls, so when the body is accumulated, the sum needs to be cleared each time
Test run screenshot:
At this time, the value of sum finally returns to 0 when the function is called again, so the processing logic is normal.
III Obviously, although the function is called multiple times, the same variable in the function body is the same area in the memory, and the variable must be initialized in the reentry function to correctly process the logic. The above processing method, in terms of The variable declaration in the function body is separated from the initial value assignment to achieve this kind of processing!
I hope to improve a little every day,