2

I want to write a function that returns an allocatable array in fortran

program test
    implicit none
    real a(3)
    real, allocatable :: F18(:)
    a = (/1,2,3/)
    print *, F18(a)
end program test

function F18(A)
implicit none
    real A(:)                   ! An assumed shape array
    real F18(size(A,1))         ! The function result itself is
                               ! the second dimension of A.  
    F18 =A                 !  
end function F18

It is expected to print "1 2 3" on the screen but I got an error:

forrtl: severe (157): Program Exception - access violation

What's the problem?

Also, I have tried code like this:

program test
    implicit none
    real a(3)
    real, allocatable :: F18(:)
    a = (/1,2,3/)
    print *, F18(a,3)
end program test

function F18(A,n)
implicit none
    integer n
    real A(:)                   ! An assumed shape array
    real F18(size(A,1))         ! The function result itself is
                               ! the second dimension of A.  
    F18 =A                 !  
end function F18

During compiling I got:

Intel(R) Visual Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 14.0.4.237 Build 20140805
Copyright (C) 1985-2014 Intel Corporation.  All rights reserved.

D:\Fortran\Elephant.f90(6): error #6351: The number of subscripts is incorrect.   [F18]
    print *, F18(a,3)
-------------^
compilation aborted for D:\Fortran\Elephant.f90 (code 1)

I am really confused with fortran's function.

What is the right way to call Array-valued Functions in fortran?

@Fortranner

1
  • For rank 1 arrays, Fortran 2003 introduces the use of square brackets, [], to delimit an array constructor. So, a = (/1,2,3/) can be written a = [1, 2, 3]. It's easier to read. Commented Sep 21, 2015 at 19:38

1 Answer 1

4

You need to make the properties of the function known to the caller. The easiest way is to put it into a module and 'use' that module. In your examples, in your main program you are declaring an array 'F18', which is not the function.

module mystuff

contains

function F18(A,n)
implicit none
    integer n
    real A(:)                   ! An assumed shape array
    real F18(size(A,1))         ! The function result itself is
                               ! the second dimension of A.
    F18 =A                 !
end function F18


end module mystuff

program test
    use mystuff
    implicit none
    real a(3)
    a = (/1,2,3/)
    print *, F18(a,3)
end program test
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! :), one more small question, I can use F18((/1.,2.,3./),3), but if I defined real A(:,:) how can I pass the array argument directly?
You can use the intrinsic reshape: F18( reshape([1,2,3,4], [2,2]) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.