Fortran

Guide To Learn

Advanced Fortran use

Operator precedence

An important aspect of operators, and a common source of bugs for novice programmers, is operator precedence. In other words, which operation gets to go first, and which last? Fortran has a few simple rules for arithmetic operator precedence: Comparison operators don’t suffer from precedence ambiguity because they operate on numeric or character values, and […]

Fortran’s built-in operators

Fortran comes with a number of built-in operators. Each falls into one of four categories, depending on what type of result they produce: There’s also the special assignment operator =. What’s special about it is that rather than returning a value of some data type, it acts to store the result on the right side into […]

Things to do with operators

There are four main aspects to Fortran operators: In this chapter, we’ll cover the built-in operators (item 1) in more detail, and we’ll learn how to express a function or subroutine as a custom operator (item 2). We’ve used built-in operators extensively since chapter 2 throughout the book but haven’t given much attention to what […]

What’s an operator?

An operator is a special creature in most programming languages. All expressions are made of some combination of literal constants, variables, function calls, and, you guessed it, operators. Like in math, operators are used to combine values (numeric or otherwise), to compute a new value. In fact, we’ve been working with operators since chapter 2 without giving […]

Writing the generic interface

At this point, we have our specific procedures implemented. Now we need to define the interface such that we can simply invoke average(temperature), average(wind_speed), and so on, rather than having to match the data types, like average_real(temperature), average_int(wind_speed), and so on. To do so, we’ll open an interface block at the top of the module, before the contains statement, that will […]

Writing the specific functions

In this subsection, we’ll implement all three specific functions, one for each data type that we intend to parse: integer, real, and logical. We’ll need the following specific functions: Let’s write the first specific function, which will operate on real arrays. Note that we already wrote this function in section 5.3, when analyzing stock price time series. The real implementation […]

The problem with strong typing

In the previous section, we learned that Fortran’s strong typing discipline prohibits passing arguments of incompatible data types to procedures. This means that when you write a function that expects a real number as input, you can’t simply pass an integer as an input argument. This would trigger a compile-time error. You can see this […]

Scroll to top