PDA

View Full Version : Fortran: Problems with dummy variables in functions


thefury
Feb16-11, 07:55 PM
I have a function that seems to be ignoring the first of 3 dummy variables. These variables are arrays with integer elements, should that be important.

For example, the call in the program is something like:
A = F(b,c,d)

and this transfers control to a function F, that is similar to:

FUNCTION F (x,y,z) RESULT (R)

IMPLICIT NONE
INTEGER :: x, y, z, R

...
operations
...

END FUNCTION F1

x takes on the value of c, y takes on the value of d and any call to use z produces a segmentation fault.

Adding another dummy variable before b (i.e. A=F(e,b,d,c) ) solves the problem with seemingly no bad effects but I'd rather have a correct function instead!

This has been bugging me for about 2 hours now and any help would be appreciated!

Mark44
Feb17-11, 10:11 AM
You need to inform the compiler that the formal parameters x, y, and z are arrays rather than scalar values.

thefury
Feb17-11, 09:03 PM
I tried that too but with no success. Thank you for the suggestion.

I have it working sensibly now, but I turned my function into a subroutine. I gather from what I've read that subroutines are somehow more adept at dealing with arrays.

Mark44
Feb18-11, 12:50 PM
I tried that too but with no success. Thank you for the suggestion.

I have it working sensibly now, but I turned my function into a subroutine. I gather from what I've read that subroutines are somehow more adept at dealing with arrays.
I don't think so. Subroutines and functions are two types of subprograms. The basic difference between the two is that a function returns some value while a subroutine causes something to happen and doesn't return a value.