USE with ONLY

Contents
Import only names
Rename on import
Avoid clashes
One use per need

Import only names

Plain use imports everything, which clutters the namespace: the only clause lists exactly the names this program needs.

With use stats, only double_it the program sees one procedure and nothing else, so stray names cannot collide.

program onlydemo implicit none use stats, only: double_it print *, double_it(21) end program onlydemo

./onlydemo

42

Rename on import

Rename imports to fit local style or dodge a clash: twice becomes a local alias for double_it with no copy and no wrapper.

The new name points at the same procedure, so every fix in the module reaches the alias at once.

program renamedemo implicit none use stats, only: twice => double_it print *, twice(21) end program renamedemo

./renamedemo

42

Avoid clashes

Two modules may export the same name: importing both plainly is ambiguous, but two narrow only lists keep them apart.

Pick step from physics and draw from graphics, and each call resolves to exactly one procedure.

program clash implicit none use physics, only: step use graphics, only: draw call step() call draw() end program clash

One use per need

Keep each use line small and local: import at the top of the unit that needs it and list only the names that unit calls.

Small import lists document dependencies at a glance and make future renames painless.

program styled implicit none use stats, only: double_it use mtools, only: triple print *, double_it(3) + triple(3) end program styled

./styled

15

Next: INTENT

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: