Clicky

Fortran Wiki
procedure (changes)

Skip the Navigation Links | Home Page | All Pages | Recently Revised | Authors | Feeds | Export |

Showing changes from revision #2 to #3: (追記) Added (追記ここまで) | (削除) Removed (削除ここまで) | (削除) Chan (削除ここまで)(追記) ged (追記ここまで)

Procedures

Fortran offers two different procedures: function and subroutine .(追記) Subroutines (追記ここまで)(追記) are (追記ここまで)(追記) more (追記ここまで)(追記) general (追記ここまで)(追記) and (追記ここまで)(追記) offer (追記ここまで)(追記) the (追記ここまで)(追記) possibility (追記ここまで)(追記) to (追記ここまで)(追記) return (追記ここまで)(追記) multiple (追記ここまで)(追記) values (追記ここまで)(追記) whereas (追記ここまで)(追記) functions (追記ここまで)(追記) only (追記ここまで)(追記) return (追記ここまで)(追記) one (追記ここまで)(追記) value. (追記ここまで)

Subroutine

A subroutine looks similar to a program except that it also takes input parameters. It is invoked by a oneline command call.

Outline

<keywords> subroutine <name> (<parameters>)
 <statements> 
end subroutine <name>

Invocation

(追記)

Subroutines must be invoked on separate lines by the call command.

(追記ここまで)
(追記) (追記ここまで)
...
 call <subroutine_name>(<parameters>)
...

Parameter intents

Each parameter must have its intent declared. The intent shows what the subroutine can do with each parameter. Possible intents are: in, out, inout.

  • in: The parameter will have an initial value and its value is not allowed to change.
  • out: The parameter will not have an initial value and one can set its value. The parameter retains the final value even after the procedure has ended.
  • inout (can also we written as in out): An initial value is set and one can change its value. The parameter retains the final value even after the procedure has ended.

Additionally, one can set the value attribute. This allows the parameter to be changed inside of the procedure but the after its end the parameter will change back to its original value. This is useful for describing C functions.

(追記) (追記ここまで)(追記)

Function

(追記ここまで)
(追記) (追記ここまで)(追記)

Functions always return at most one value which can be of default type or derived type.

(追記ここまで)
(追記) (追記ここまで)(追記)

Outline

(追記ここまで)
(追記) (追記ここまで)(追記)

There are multiple ways to write identical functions. One method is

(追記ここまで)
(追記) (追記ここまで)(追記)
<keywords> function <func_name> (<parameters>) result (<res_name>)
 <statements> 
end function <func_name>
(追記ここまで)
(追記) (追記ここまで)(追記)

The variable res_name has automatically the intent out.

(追記ここまで)
(追記) (追記ここまで)(追記)

Invocation

(追記ここまで)
(追記) (追記ここまで)(追記)

Functions can be invoked in the same manner as built-in functions, e.g. sin. The following simplified example tries to show an invocation for a function some_function which returns a real value.

(追記ここまで)
(追記) (追記ここまで)(追記)
...
 use some_module, only : some_function
 real :: x = 1, y
 y = x**3 + 3*x*some_function(x)
...
(追記ここまで)

Keywords

Recursive

Only procedures with the keyword recursive are permitted to call themselves.

Elemental

For easy vectorization use the keyword elemental. See also elemental.

Pure

The pure keyword indicates that the procedure has no side effects, e.g. does not alter gloabl variables, or prints messages.

Revised on July 4, 2018 08:33:15 by Mr Blue? (91.16.88.222) (2718 characters / 1.0 pages)
Edit | Back in time (2 revisions) | Hide changes | History | Views: Print | TeX | Source | Linked from: Keywords

AltStyle によって変換されたページ (->オリジナル) /