Modules at Scale
Private by Default
Public Surface
A larger module is easier to maintain when names are private by default and only the intended API is public.
Program
Play the program to choose a count and call the one public formatting function.
private_public.f90
module catalog_mod
implicit none
private
public :: label_count
contains
function label_count(count) result(label)
integer, intent(in) :: count
character(len=16) :: label
write(label, '(A,I0)') 'items=', count
end function label_count
end module catalog_mod
program private_public_demo
use catalog_mod, only: label_count
implicit none
integer :: count
character(len=16) :: label
count =
label = label_count(count)
print '(A)', trim(label)
end program private_public_demo
module catalog_mod
implicit none
private
public :: label_count
contains
function label_count(count) result(label)
integer, intent(in) :: count
character(len=16) :: label
write(label, '(A,I0)') 'items=', count
end function label_count
end module catalog_mod
program private_public_demo
use catalog_mod, only: label_count
implicit none
integer :: count
character(len=16) :: label
count =
label = label_count(count)
print '(A)', trim(label)
end program private_public_demo
module catalog_mod
implicit none
private
public :: label_count
contains
function label_count(count) result(label)
integer, intent(in) :: count
character(len=16) :: label
write(label, '(A,I0)') 'items=', count
end function label_count
end module catalog_mod
program private_public_demo
use catalog_mod, only: label_count
implicit none
integer :: count
character(len=16) :: label
count =
label = label_count(count)
print '(A)', trim(label)
end program private_public_demo
private
`private` hides module names unless they are explicitly exported.
public API
`public :: label_count` keeps one supported entry point visible.
character result
A function can return a fixed-length character value and callers can `trim` it.