array valued functions
Author Message array valued functions

hi,

i am trying to write a simple function that takes two arrays
and returns me their sum vector. i want to do it using a function
and not a subroutine. I tried the following

FUNCTION VECP(A, B)

IMPLICIT DOUBLE PRECISION (A - Z)

DIMENSION A(3), B(3)
DOUBLE PRECISION, DIMENSION(3) :: VECP

VECP(1) = A(1)+B(1)
VECP(2) = A(2)+B(2)
VECP(3) = A(3)+B(3)

RETURN
END

and i am calling it from another subroutine in the same file as
follows
DIMENSION                RHS(9), DMY1(3), DMY2(3), DMY3(3)

EXTERNAL VECP

DMY1(1:3) = 1D0
DMY2(1:3) = 2D0

DMY3 = VECP(DMY1,DMY2)

it gives me these warnings which i do not understand

explicit-shaped array is being passed to a routine that expects a
pointer or assumed-shape array
DMY3(1:3) = VECP(DMY1(1:3),DMY2(1:3))
--------^
the call to VECP, there is no actual argument corresponding to the dummy
argument B.
DMY3(1:3) = VECP(DMY1(1:3),DMY2(1:3))
--------^
Routine VECP called with different number and/or type of actual
arguments in earlier call - C attribute required if intended.
DMY3(1:3) = VECP(DMY1(1:3),DMY2(1:3))
--------^

kw_dyn4.obj - 0 error(s), 3 warning(s)

i am using compaq visual fortran, cd someone please enlighten me
what's happening and how to get over them!! thanks in advance

--
http://www.*-*-*.com/

Tue, 13 Dec 2005 06:45:59 GMT  array valued functions

Quote:

> i am trying to write a simple function that takes two arrays
> and returns me their sum vector.

I assume you are doing this only as an excercise and realize
that this capability is already built into f90.  Just write

DMY3 = DMY1 + DMY2

Quote:
>    IMPLICIT DOUBLE PRECISION (A - Z)

I strongly recommend against this.  It isn't the cause of your
problem today, but it will be the cause of later problems....all
but guaranteed.

Quote:
>    EXTERNAL VECP

This isn't going to work and may be your real problem.
Array-valued functions must have explicit interfaces where
they are invoked from; the EXTERNAL statement doesn't give you one.
You need to either

1. Put the function in a module an dUSE that module in the main.

2. Make it an internal function of the main.

or

3. Write an interface body and put that in the main.

I recommend against option 3, though it seems to ebe the first one
that some people try.  It is awkward, ugly, and error-prone.

Quote:
> it gives me these warnings which i do not understand

I don't quite understand the messages either, but you'll need to
fix the above-mentioned problem anyway.  Perhaps it is just
confusing the compiler.

Quote:
>  DMY3(1:3) = VECP(DMY1(1:3),DMY2(1:3))

This line doesn't seem to match your code.  (The code doesn't have the
(1:3) parts).  Is the above code actually what gave this error.
Not that it woul dbe much clearere anyway, but that's just an extra
puzzle.

--
Richard Maine                       |  Good judgment comes from experience;
email: my first.last at org.domain  |  experience comes from bad judgment.
org: nasa, domain: gov              |        -- Mark Twain

Tue, 13 Dec 2005 07:05:02 GMT  array valued functions

Quote:

> i am trying to write a simple function that takes two arrays
> and returns me their sum vector. i want to do it using a function
> and not a subroutine. I tried the following

>    FUNCTION VECP(A, B)

You need a RESULT clause in order to pass back the sum to
the invoking statement.
e,g, FUNCTION VECP (A, B) RESULT (VECP)
Quote:
>    IMPLICIT DOUBLE PRECISION (A - Z)

>    DIMENSION A(3), B(3)
>    DOUBLE PRECISION, DIMENSION(3) :: VECP

>    VECP(1) = A(1)+B(1)
>    VECP(2) = A(2)+B(2)
>    VECP(3) = A(3)+B(3)

>    RETURN
>    END

Tue, 13 Dec 2005 17:53:27 GMT  array valued functions

Quote:

> > i am trying to write a simple function that takes two arrays
> > and returns me their sum vector. i want to do it using a function
> > and not a subroutine. I tried the following

> > FUNCTION VECP(A, B)

> You need a RESULT clause in order to pass back the sum to
> the invoking statement.
> e,g, FUNCTION VECP (A, B) RESULT (VECP)

> > IMPLICIT DOUBLE PRECISION (A - Z)

> > DIMENSION A(3), B(3)
> > DOUBLE PRECISION, DIMENSION(3) :: VECP

> > VECP(1) = A(1)+B(1)
> > VECP(2) = A(2)+B(2)
> > VECP(3) = A(3)+B(3)

> > RETURN
> > END

Above is wrong..
You can not use function's name as target of the result clause
(at least using CVF compiler you cant).
Plus you are wrong that a result clause is needed, to make above
compile ok, just eliminate the result clause.

As a Fortran book author I'm surprised you didnt tell him to just use
C = A + B  without the use of a function,
even PL/I allows direct array math syntax, right?

Tue, 13 Dec 2005 19:36:19 GMT  array valued functions

Quote:

> > > i am trying to write a simple function that takes two arrays
> > > and returns me their sum vector. i want to do it using a function
> > > and not a subroutine. I tried the following

> > > FUNCTION VECP(A, B)

> > You need a RESULT clause in order to pass back the sum to
> > the invoking statement.
> > e,g, FUNCTION VECP (A, B) RESULT (VECP)

Oops, hadn't noticed that VECP is name of function.

Quote:
> > > IMPLICIT DOUBLE PRECISION (A - Z)

> > > DIMENSION A(3), B(3)
> > > DOUBLE PRECISION, DIMENSION(3) :: VECP

> > > VECP(1) = A(1)+B(1)
> > > VECP(2) = A(2)+B(2)
> > > VECP(3) = A(3)+B(3)

> > > RETURN
> > > END

> Above is wrong..

> I'm surprised you didnt tell him to just use
> C = A + B  without the use of a function,

he wanted a function that returned an array result.

Quote:
> even PL/I allows direct array math syntax, right?

It has, for 38 years.

Tue, 13 Dec 2005 22:10:16 GMT  array valued functions

Quote:

> > > > i am trying to write a simple function that takes two arrays
> > > > and returns me their sum vector. i want to do it using a
function
> > > > and not a subroutine. I tried the following

> > > > FUNCTION VECP(A, B)

> > > You need a RESULT clause in order to pass back the sum to
> > > the invoking statement.
> > > e,g, FUNCTION VECP (A, B) RESULT (VECP)

> Oops, hadn't noticed that VECP is name of function.

> > > > IMPLICIT DOUBLE PRECISION (A - Z)

> > > > DIMENSION A(3), B(3)
> > > > DOUBLE PRECISION, DIMENSION(3) :: VECP

> > > > VECP(1) = A(1)+B(1)
> > > > VECP(2) = A(2)+B(2)
> > > > VECP(3) = A(3)+B(3)

> > > > RETURN
> > > > END

> > Above is wrong..

> > I'm surprised you didnt tell him to just use
> > C = A + B  without the use of a function,

> he wanted a function that returned an array result.

A nice try implying you have corrected the poster's function, but when
you remove your erroneous result clause, we are left with poster's
ORIGINAL function, which was and is correct..

allocated array result from a function?

Tue, 13 Dec 2005 23:06:23 GMT  array valued functions

! Date: Fri, 27 Jun 2003 00:45:59 +0200

! hi,
! i am trying to write a simple function that takes two arrays
! and returns me their sum vector. i want to do it using a function
! and not a subroutine. I tried the following

You'll need an expicit interface and a change to the declarations
of the dummy arguments:--

PROGRAM FUN
IMPLICIT NONE
REAL :: RHS(9), DMY1(3), DMY2(3), DMY3(3)
DMY1(1:3) = 1D0
DMY2(1:3) = 2D0
DMY3 = VECP(DMY1,DMY2)
PRINT *, DMY3

CONTAINS

FUNCTION VECP(A, B)
IMPLICIT NONE
REAL, INTENT (IN) :: A(:), B(:)
REAL, DIMENSION(3) :: VECP

VECP(1) = A(1)+B(1)
VECP(2) = A(2)+B(2)
VECP(3) = A(3)+B(3)
RETURN
END FUNCTION VECP
END PROGRAM FUN

Having the function CONTAINed in the main program (as is the case here)
simplifies the interface.

! and i am calling it from another subroutine in the same file as
! follows
!       DIMENSION                RHS(9), DMY1(3), DMY2(3), DMY3(3)
!       EXTERNAL VECP
!       DMY1(1:3) = 1D0
!       DMY2(1:3) = 2D0
!       DMY3 = VECP(DMY1,DMY2)
! it gives me these warnings which i do not understand
! explicit-shaped array is being passed to a routine that expects a
! pointer or assumed-shape array
!  DMY3(1:3) = VECP(DMY1(1:3),DMY2(1:3))
! --------^
! the call to VECP, there is no actual argument corresponding to the dummy
! argument B.
!  DMY3(1:3) = VECP(DMY1(1:3),DMY2(1:3))
! --------^
! Routine VECP called with different number and/or type of actual
! arguments in earlier call - C attribute required if intended.
!  DMY3(1:3) = VECP(DMY1(1:3),DMY2(1:3))
! --------^
! i am using compaq visual fortran, cd someone please enlighten me
! what's happening and how to get over them!! thanks in advance

Wed, 14 Dec 2005 22:13:24 GMT  array valued functions

Quote:

> You'll need an expicit interface and a change to the declarations
> of the dummy arguments:--

Yes, the explicit intgerface is needed, as mentioned in other replies
in this thread (at least in mine - though I didn't show code to do so).

However, changing the dummy arguments to assumed shape is not needed,
and I wouldn't even think of doing it the way robin shows here.  I
generally do prefer assumed shape dummy arguments, but certainly not
like this

Quote:
> FUNCTION VECP(A, B)
>    IMPLICIT NONE
>    REAL, INTENT (IN) :: A(:), B(:)
>    REAL, DIMENSION(3) :: VECP

>    VECP(1) = A(1)+B(1)
>    VECP(2) = A(2)+B(2)
>    VECP(3) = A(3)+B(3)
>    RETURN
> END FUNCTION VECP

Here the dummies are declared assumed shape, but the function result
and the body of the code are hard-wired to specific sizes.  Nothing
good can come of this.  Make declarations agree with the actual usage.
In this case, the actual usage is hard-wired to a size of 3.

If you want to use assumed shape, that's fine, but in that case,
make the function return shape depend on that of the arguments
and make the body loop also do so.

--
Richard Maine
email: my last name at domain
domain: isomedia dot com

Wed, 14 Dec 2005 23:52:36 GMT

 Page 1 of 1 [ 8 post ]

Relevant Pages