Fortran

Guide To Learn

Modules with Public and Private Components

This example demonstrates the use of public and private components in Fortran modules.

fortranCopy codemodule my_module
    implicit none
    private

    integer, public :: public_var
    real :: private_var

contains
    subroutine set_vars(val1, val2)
        integer, intent(in) :: val1
        real, intent(in) :: val2
        public_var = val1
        private_var = val2
    end subroutine set_vars

    function get_private_var() result(result)
        result = private_var
    end function get_private_var

end module my_module

program use_module
    use my_module
    implicit none

    call set_vars(42, 3.14)

    print *, 'Public variable: ', public_var
    print *, 'Private variable (via function): ', get_private_var()
end program use_module
Modules with Public and Private Components

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top