Skip to main content
Engineering LibreTexts

15.6: Functions

  • Page ID
    54362
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    A function is a special type of Fortran subprogram that is expected to return a single result or answer. A function will typically accept some kind of input information and based on that information, return a result. The two types of Fortran functions are described in the following sections.

    Intrinsic Functions

    As described previously, an intrinsic function is a built-in function that is already available. Some of the intrinsic functions already described include sin(), cos(), tan(), real(), int(), and nint(). A more comprehensive list is contained in Appendix D.

    User-Defined Functions

    A user-defined function is a function that a written by the user for specific or specialized requirements. The general form of a user-defined function is as follows:

    <type> function <name> ( <arguments> )
    <declarations>
    
        <body of function>
    
        <name> = expression
        return
    end function <name>
    

    The <type> is one of the Fortran data types: real, integer, logical, character, or complex. It is possible to place the type declaration on a separate line from the function statement.

    The information, in the form of arguments, is passed from the calling routine to the function. Each of the passed arguments must be declared and the declaration must include the type and the intent. The arguments in the calling routine and the function must match and are matched up by position.

    An example function to convert a Fahrenheit temperature to Celsius temperature would be as follows:

    real function fahr_to_celsius(ftemp)
    real, intent(in) :: ftemp
    
        fahr_to_celsius = (ftemp – 32.0) / 1.8
    
        return
    end function fahr_to_celsius
    

    Which, given a Fahrenheit temperature, will return the Celsius temperature. The single input argument, ftemp, is declared to be a real value and “intent(in)”, which means that the value is expected to be coming into the function and cannot be changed. The final value is returned to the calling routine by assigning a value to the function name, fahr_to_celsius, in this example.

    Side Effects

    A side-effect is when a function changes one or more of its input arguments. Since the arguments can be declared as “intent(out)” or “intent(inout)”, the function could change the arguments. In general, this is considered poor practice and should be avoided. None of the examples in this text will include or utilize side-effects.


    This page titled 15.6: Functions is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?