Fortran

Guide To Learn

When do you use a subroutine over a function?

Whenever I write a new procedure, I use the rules of thumb shown in figure 3.8 to decide whether to make it a function or a subroutine. Figure 3.8 Deciding when to use a subroutine over a function This is a simple decision-making process that you can follow. If you know your procedure will cause […]

Defining and calling a subroutine

Let’s see the difference between a subroutine and a function in an example. The following listing defines a subroutine add that’s equivalent to our function sum from the previous subsection. Listing 3.13 A subroutine that calculates the sum of two integers ❶ Inputs arguments like before ❷ Outputs argument that’s returned to the caller Here, a and b are input arguments–notice the intent(in) attribute just like in the sum function–and res is the […]

Modifying program state with subroutines

I mentioned earlier that Fortran has two kinds of procedures: functions and subroutines. Many rules that we covered for functions apply to subroutines as well. They’re both designed to be reused many times, and both may have input and output arguments. Unlike functions, subroutines can’t be used in expressions and can only be invoked in a […]

Expressing finite difference as a function in the tsunami simulator

You now understand how to define a function and how to call it from the main program. Finally, we get to the fun part–applying our new knowledge about functions to refactor our tsunami simulator. Let’s look back at the main time loop in our program, as reprised in the following listing. Listing 3.9 The time […]

Your first function

Let’s jump straight into it and write our first custom function. We’ll take our cold front program from listing 3.2 and rewrite it to delegate the temperature calculation to an external function. This will allow us to easily compute the solution for a series of different input values. Specifically, we’ll iterate over several values of […]

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 […]

An overview of Fortran program units

When I introduced program as the main Fortran program unit in the previous chapter, I also mentioned a few others: functions, subroutines, and modules. Functions and subroutines, which are the focus of this chapter, are both kinds of procedures. A procedure is a miniprogram that can be called any number of times from the main program, or […]

Revisiting the cold front problem

In the previous chapter (section 2.2.2), I introduced the example of a cold front to illustrate the concepts of temperature gradient (change in space) and tendency (change in time). There, I asked you to calculate the change of temperature in Miami, considering the temperatures in Atlanta and Miami, the distance between them, and the speed of the front (figure 3.4). Figure […]

Refactoring the tsunami simulator

In the previous chapter, we made the first working version of what will become a realistic water wave simulator. Contained in a single program, it included data declaration and initialization, arithmetic expressions and assignment to calculate the solution, a do loop to advance the solution forward in time, and a print statement to output the results to screen. With 26 lines […]

Toward higher app complexity

Simple is better than complex. Complex is better than complicated.  –Tim Peters, The Zen of Python Although a mantra of Python, the opening quote applies well to Fortran and programming in general. We always aim for simple, whenever possible. This is especially true in software design, where we often deal with increasingly complex systems. Simple […]

Scroll to top