Fortran Fortran: Problems with dummy variables in functions

Click For Summary
The discussion revolves around a programming issue where a function is ignoring the first of three dummy variables, leading to a segmentation fault when attempting to use the third variable. The user initially calls the function with three parameters but encounters problems until they add an extra dummy variable, which resolves the issue without clear explanation. The user expresses frustration and seeks help, mentioning attempts to inform the compiler about the parameters being arrays rather than scalars, which did not resolve the problem. They later report success after converting the function to a subroutine, noting that subroutines may handle arrays better. However, there is a clarification that the distinction between functions and subroutines lies in their return behavior, with functions returning values and subroutines performing actions without returning values.
thefury
Messages
7
Reaction score
0
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:

Code:
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!
 
Technology news on Phys.org
You need to inform the compiler that the formal parameters x, y, and z are arrays rather than scalar values.
 
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.
 
thefury said:
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 59 ·
2
Replies
59
Views
11K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 12 ·
Replies
12
Views
3K