Fortran

Guide To Learn

Don’t repeat yourself, use procedures

Like I mentioned earlier, procedures allow you to define snippets of code as their own self-contained units of functionality. You can then use them and reuse them as much as you need, by passing different values of input parameters and getting the results back. They’re similar to the main program, in that they can include any declarative and executable code. Unlike the main program, you have to call a procedure from a parent program or another procedure. In other words, you can’t just compile a procedure on its own and run it from the command line. Procedures give you the power to define some piece of functionality once, then reuse it as many times as you need by invoking that procedure.

As a general rule of thumb, we’ll write any reusable code as functions and resort to subroutines only when we must. This simple principle will help us write simpler programs that are easier to understand and debug.

Tip If you find yourself writing the same piece of code more than a few times, consider making it a procedure.

Don’t repeat yourself, use procedures

Leave a Reply

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

Scroll to top