0% found this document useful (0 votes)
20 views

FTN77 User Defined Functions

This document discusses user-defined functions in Fortran 77. It explains that functions carry out a task and return a value, like predefined functions, but must be written by the programmer. A function declaration includes the return type, function name, and arguments. In the function body, the arguments are declared and the return value is assigned. A typical example calculates the area of a rectangle by multiplying its length and width in a function called from the main program.

Uploaded by

rodwellhead
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

FTN77 User Defined Functions

This document discusses user-defined functions in Fortran 77. It explains that functions carry out a task and return a value, like predefined functions, but must be written by the programmer. A function declaration includes the return type, function name, and arguments. In the function body, the arguments are declared and the return value is assigned. A typical example calculates the area of a rectangle by multiplying its length and width in a function called from the main program.

Uploaded by

rodwellhead
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

www.computing.me.

uk

Fortran 77: User-defined Functions


It has always been good programming practice to divide a computer program up into blocks of code that were as independent of each other as possible. These blocks were termed modules and the division of code in this way is often called procedural programming or modular programming1. In Fortran modules are collectively termed subprograms and there are two main types of subprogram; a subroutine which carried out a defined task and a function, which carried out a task but with a purpose of returning a value. In this document we consider the use of functions in Fortran. There are already a number of pre-defined functions in Fortran2. User-defined functions have a similar format to predefined functions, in that they require a set of values as input and return an output value, but they must be either written by the programmer or be supplied by an external library. Note that in Fortran 77 only a single value of any data type can be returned from a function; it is not possible to return an array. A function declaration is of the following form.
<return type> <function identifier>(<arguments>) <delaration of arguments> : <function identifier> = <expression> END

The return type is required only if the function identifiers type is not implicitly defined3. But it is good practice to state the return type in all function calls. In the body of the function the arguments are declared. Usually at the end of the coding for the function, the return value is assigned to the function identifier. When the function is called, the types of the function identifier must be declared if it is not implicitly defined. The types of the arguments must also match those in the function declaration. A typical function for calculating the area of a rectangle is defined here, and a call to the function from a main program and the output is shown.
C Functions demo PROGRAM AREADEMO REAL*4 ALENGTH, AWIDTH ALENGTH=3.0 AWIDTH=2.0 WRITE(*,*) 'AREA= ',AREA(ALENGTH,AWIDTH) END C Function RECAREA returns the area of a rectangle C given its length and width REAL*4 FUNCTION AREA(LENGTH,WIDTH) REAL*4 LENGTH,WIDTH AREA=LENGTH*WIDTH END

Modular Programming Intrinsic Functions 3 Variables and Identifiers


1 2

You might also like