Fortran Programming Course - Procedures, Pointers and Modules

Опубликовано: 05 Август 2026
на канале: Desmus
85
3

In this video we are going to see the use of procedures, pointers and modules.

Procedures
Procedures are blocks of code that could be reused by calling them from other code sections.
Fortran has two forms of procedure:
Subroutines
Functions

Subroutines
Subroutines are procedures that doesn't return any value when its execution ends, this means that subroutines are implemented when it is necessary to execute a certain task without notifying any value at the end of its execution.
The format for a subroutine is the following:
subroutine subroutine_name(parameter_1, parameter_2, …)
block of code
end subroutine subroutine_name
Parameters could be avoided if not needed. All subroutines must include the implicit none instruction.
To call a subroutine we use the instruction call as the following example:
call subroutine_name(argument_1, argument_2, …)

Functions
Functions are procedures that return a value when its execution ends, this means that functions are implemented when it is necessary to execute a certain task to notify a value at the end of its execution.
The format for a function is the following:
function function_name(parameter_1, parameter_2, …) result(norm)
block of code
norm = value
end function function_name
Parameters could be avoided if not needed. All functions must include the implicit none instruction.
To call a function we use the function name as the following example:
variable_name = function_name(argument_1, argument_2, …)

Dummy Arguments vs. Arguments
Dummy Arguments (or Parameters) are variables specified while procedure definition, and arguments are variables specified while procedure call.
Example of dummy arguments:
function add(x, y) result(norm)
block of code
norm = x + y
end function add
Example of arguments:
result = add(1,2)

Read and Write Dummy Arguments
Dummy arguments could have read and write access, read access or write access.
This could be expecified with the intent(inout), intent(in), intent(out) variable attribute.
An example of dummy arguments:
function add(x, y) result(norm)
integer, intent(in) :: x
integer, intent(in) :: y

norm = x + y
end function add
The functions that have all its dummy arguments as intent(in) are called pure functions.
If we need to modify arguments value we are going to use a subroutine.
All arguments are called by reference, this means that the procedure could change the value of the argument variable if not specified as intent(in). If we want a procedure to be called by value we need to specify the value attribute.

Pass by Value vs. Pass by Reference
Pass by value means that the arguments of a procedure could not be changed outside the procedure scope. Arguments are just a copy of real variable values.
Pass by reference means that the arguments of a procedure could be changed outside the procedure scope. Arguments are a link (pointer) of real variable values.

Pointers
Pointers are variables that store memory addresses of specific data types.
A pointer variable is a memory address that stores another memory address. A variable is a memory address that stores a value type.

Associated and Nullify Pointers
We can check if a pointer is associated to a certain variable with the associated procedure call.
We can also break the link between pointer and variable with the nullify procedure call.

Modules
Modules are separated pieces of code that contain definitions that are accessible to programs, procedures, and other modules through the use statement. They can contain data objects, type definitions, procedures and interfaces.
It is recommended to always place functions and subroutines within modules.
An advantage of placing subroutines and functions in modules is that they can have optional arguments. In a procedure with an argument declared optional, the present function is used to test if the argument was set in the caller procedure.

Slides Document: https://docs.google.com/presentation/...
Source Code: https://github.com/desmusapplication/...

Social Media
Facebook: https://web.facebook.com/profile.php?...
X: https://x.com/DesmusApp

Music: Pold - Calm (me) Down