Solution 1
You don't need to store anything if you make LocalSub internal to H:
subroutine H(sub)
external:: sub
call G(LocalSub)
contains
subroutine LocalSub
message='Good Afternoon'
call G(sub)
end subroutine LocalSub
end subroutine H
It requires Fortran 2008.
With some cleanup, indentation for readability, removing the ugly externals using an abstract interface (Fortran 2003, but external can be avoided even in Fortran 90 using interface blocks) the code is:
module G_MOD
implicit none
character(len=30)::message='Good Morning'
abstract interface
subroutine sub_interface
end subroutine
end interface
contains
subroutine G(Sub)
procedure(sub_interface) :: sub
call Sub
end subroutine G
end module G_MOD
module H_MOD
use G_MOD
implicit none
contains
subroutine H(sub)
procedure(sub_interface) :: sub
call G(LocalSub)
contains
subroutine LocalSub
message='Good Afternoon'
call G(sub)
end subroutine LocalSub
end subroutine H
end module H_MOD
program test
use H_MOD
implicit none
call H(MySub)
contains
subroutine MySub
use G_MOD,only:message
write(*,*)trim(Message)
end subroutine MySub
end program test
Solution 2
If you really want to store a reference to the procedure in a module, it is possible, but remember global variables are UGLY. What if you want to make multiple calls to your optimization in parallel?
So, you can store the address (not name) of a procedure in a procedure pointer. These require Fortran 2003. A minimal change of your code is
module H_MOD
use G_MOD
implicit none
procedure, pointer :: stored_sub => null()
contains
subroutine H(sub)
external:: sub
stored_sub => sub
call G(LocalSub)
end subroutine H
subroutine LocalSub
message='Good Afternoon'
call G(stored_sub)
end subroutine LocalSub
end module H_MOD
but much better modern code is:
module G_MOD
implicit none
character(len=30)::message='Good Morning'
abstract interface
subroutine sub_interface
end subroutine
end interface
contains
subroutine G(Sub)
procedure(sub_interface) :: sub
call Sub
end subroutine G
end module G_MOD
module H_MOD
use G_MOD
implicit none
procedure(sub_interface), pointer :: stored_sub => null()
contains
subroutine H(sub)
procedure(sub_interface) :: sub
stored_sub => sub
call G(LocalSub)
end subroutine H
subroutine LocalSub
message='Good Afternoon'
call G(stored_sub)
end subroutine LocalSub
end module H_MOD
module MySub_module
contains
subroutine MySub
use G_MOD,only:message
write(*,*)trim(Message)
end subroutine MySub
end module MySub_module
program test
use H_MOD
use MySub_module
implicit none
call H(MySub)
end program test
Still, I definitely prefer the variant with the internal procedure.
And remember, use indentation, it is essential for readable code.