Title here
Summary here
Here’s a function that will take an arbitrary number of int
values as arguments.
program variadic_functions
implicit none
integer :: total, i
integer, dimension(:), allocatable :: nums
call sum([1, 2])
call sum([1, 2, 3])
allocate(nums(4))
nums = [1, 2, 3, 4]
call sum(nums)
contains
subroutine sum(nums)
integer, dimension(:), intent(in) :: nums
integer :: i, total
print *, nums
total = 0
do i = 1, size(nums)
total = total + nums(i)
end do
print *, total
end subroutine sum
end program variadic_functions
To compile and run this program, save the code in a file named variadic_functions.f90
and use a Fortran compiler.
$ gfortran -o variadic_functions variadic_functions.f90
$ ./variadic_functions
1 2
3
1 2 3
6
1 2 3 4
10
Now that we can run and build basic Fortran programs, let’s learn more about the language.