Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function Returning an array in Fortran

Tags:

It is my understanding that you can return an array from a function in Fortran, but for some reason my code is only returning the first value in the array I am asking it to return. This is the function:

function polynomialMult(npts,x,y)     integer npts     double precision x(npts), results(npts + 1), y(npts,npts)      polynomialMult =  x(1:npts) + 1  end function 

and this is where I'm calling it

 C(1:numPoints) = polynomialMult(numPoints,x,f)  print *, C(1:numPoints)` 

right now it doesn't do anything useful because I am trying to understand the syntax before I write the logic. I saw some stuff about specifying types for functions, but when I write

integer function polynomialMult(npts,x,y) 

or whatever I get a compilation error.

like image 394
Statler Avatar asked Sep 30 '10 05:09

Statler


People also ask

How do I return an array from a subroutine in Fortran?

In Fortran, I can return arrays from a subroutine with three approaches. The first one is via an intent(out) parameter. The second one is via a function having the array as result . The third one is to have a function having as result a pointer to array, which is allocated in the function.

What is return in Fortran subroutine?

Return (in Subroutines) A return statement in a subroutine instructs Fortran to terminate the subroutine and return to the main program at the point where it departed. Thus it works like a stop statement in the main program, halting the program prematurely before the final end statement.

How do you define a function in Fortran?

The purpose of a function is to take in a number of values or arguments, do some calculations with those arguments and then return a single result. There are some functions which are written into FORTRAN and can be used without any special effort by you, the programmer. They are called intrinsic functions.


1 Answers

To define a function which returns an array include the function declaration inside the function, like this:

function polynomialMult(npts,x,y)     integer npts     double precision x(npts), results(npts + 1), y(npts,npts)  ! Change the next line to whatever you want     double precision, dimension(npts) :: polynomialMult      polynomialMult =  x(1:npts) + 1  end function 

Your declaration

integer function polynomialMult(npts,x,y) 

declares that the function returns an integer. An integer, not an array of integers. I don't think the standard allows function declarations such as:

integer, dimension(10) function polynomialMult(npts,x,y) 

but I could be wrong. I always use the form I showed you above.

If you have an up to date Fortran compiler you can do clever things such as return an allocated array. And I suggest you figure out array syntax. For example, your statement:

polynomialMult =  x(1:npts) + 1 

could more concisely be written:

polynomialMult =  x + 1 

since Fortran will map the scalar addition to all elements of the array x which you have declared to have only npts elements.

Passing the sizes of arrays into subroutines is very FORTRAN77 and almost always unnecessary now. Generally you either want to operate on every element in an array (as in the array syntax example) or you should let the subprogram figure out the size of the array it is dealing with.

like image 128
High Performance Mark Avatar answered Sep 19 '22 23:09

High Performance Mark