concept function in category fortran

appears as: functions, function, function, A function, The function, functions
Modern Fortran: Building efficient parallel applications MEAP V13

This is an excerpt from Manning's book Modern Fortran: Building efficient parallel applications MEAP V13.

As we work through this book, we’ll come across new layers of abstraction in each chapter. Here, an abstraction is a programming mechanism that aims to black-box the internal implementation away from the programmer. For example, in the next chapter, functions and subroutines are an abstraction over explicit, imperative code. In chapter 8, you’ll learn about derived types, which can contain any number of variables and procedures attached to them, and this is yet another layer of abstraction.

Figure  3.3. Using a module and a function to reuse and simplify code. Module mod_diff, which defines the difference function diff, is accessed from the main program with the use statement (top arrow). Through use association, the function diff can be used within the scope of the main program (bottom arrow).
ch03 app code module

In this new framework, we define the reusable data and functions inside the module. The module is then accessed from the main program with the use statement. We’ll first refactor our minimal working app to compute the finite difference in a function, while exactly reproducing the existing results. Then, in the next chapter, we’ll define our new custom module to host our functions, and we’ll expand our app to produce more realistic simulations.

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 from another procedure. Like the main program, procedures have executable code, the code that does things. Figure 3.5 illustrates functions and subroutines.

Figure  3.5. Overview of a function and a subroutine, and how they’re invoked from the main program
fortran procedures overview

Like when declaring variables in the main program, you can specify input arguments of the same data type on the same line. Furthermore, you can specify the data type of the function result immediately in front of the word function, as shown in the following listing. Notice that we use both of these features in the cold front program as well.

Listing 3.5. Specifying the data type of the function result in the function statement
language="fortran" linenumbering="unnumbered">integer function sum(a, b) #1
  integer, intent(in) :: a, b #2
  sum = a + b
end function sum

It’s also possible, for convenience, to specify a different name for the function result, other than the name of the function, using the result attribute, as the following listing demonstrates.

Listing 3.6. Specifying the function result as different from the function name
language="fortran" linenumbering="unnumbered">integer function sum(a, b) result(res) #1
  integer, intent(in) :: a, b
  res = a + b #2
end function sum

The advantage to using the result attribute may not be obvious from this example because the name of the function (sum) is already quite short, but it comes in handy for longer function names. Note that Fortran comes with an intrinsic (built-in) function sum that returns the sum of all elements in an input array. Because of this, some compilers may warn you if you compile this function, and that’s okay. I used the same name for the example in this section only for convenience.

In listing 3.6, the function returns a single scalar as a result. In general, functions can return a scalar, an array, or a more complex data structure, but it’s always a single entity.

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest