Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29386 Discussions

How can I have an internal subroutine?

WSinc
New Contributor I
725 Views
Hello-

I have a block of code WITHIN a routine that I execute fromseveral different places.

Rather than have a messy setup of assigned GOTO statements, is there a way to call it internally

from somewhere else in the same routine?

Example: (this doesn't work, but it illustrates what I'm talking about).

Subroutine SUB1

call sub2
return

subroutine sub2
la-di-da-di dah
return
end ! sub1

end ! sub2

Notice that here I don't give any arguments, but sometimes it might be nice to do this with a calling sequnece.
I don't want sub2 to be accessable from anywhere else
Is this where a MODULE would be used?
0 Kudos
3 Replies
TimP
Honored Contributor III
725 Views
Any Fortran textbook of the last 20 years, including those on line, will help. Both module and internal procedures appear even on wikipedia
0 Kudos
Steven_L_Intel1
Employee
725 Views
This is where you would use an "internal subprogram", a feature of Fortran 90. The general syntax is:

subroutine SUB1
call sub2
return

contains

subroutine sub2
...
return
end subroutine sub2
end

The key points here are the line "contains", following which are your internal subprograms. The internal subprograms can see variables in the parent. You are also required to use end subroutine (or end function) and not just end, to end an internal subprogram.

I don't think a module is right for what you want.
0 Kudos
WSinc
New Contributor I
725 Views
Thanks for the example ! !

It clarifies things a LOT better.
0 Kudos
Reply