Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To define a function which returns an array include the function declaration inside the function, like this:</p> <pre><code>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 </code></pre> <p>Your declaration</p> <pre><code>integer function polynomialMult(npts,x,y) </code></pre> <p>declares that the function returns an integer. <em>An integer</em>, not an array of integers. I don't think the standard allows function declarations such as:</p> <pre><code>integer, dimension(10) function polynomialMult(npts,x,y) </code></pre> <p>but I could be wrong. I always use the form I showed you above.</p> <p>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:</p> <pre><code>polynomialMult = x(1:npts) + 1 </code></pre> <p>could more concisely be written:</p> <pre><code>polynomialMult = x + 1 </code></pre> <p>since Fortran will map the scalar addition to all elements of the array x which you have declared to have only <code>npts</code> elements. </p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload