Fortran: Problems with dummy variables in functions

Click For Summary

Discussion Overview

The discussion revolves around issues encountered with dummy variables in Fortran functions, particularly focusing on the handling of array parameters and the resulting segmentation faults. Participants explore the differences between functions and subroutines in this context.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a function that seems to ignore the first of three dummy variables, leading to a segmentation fault when accessing one of the variables.
  • Another participant suggests that the compiler needs to be informed that the formal parameters are arrays rather than scalars.
  • A participant mentions that they resolved the issue by converting the function into a subroutine, implying that subroutines may handle arrays more effectively.
  • There is a clarification that functions and subroutines serve different purposes, with functions returning values and subroutines performing actions without returning values.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness of functions versus subroutines in handling arrays, and the discussion does not reach a consensus on the best approach to resolve the initial problem.

Contextual Notes

There are unresolved assumptions regarding the specific nature of the dummy variables and the context in which the function is called, as well as the potential implications of the segmentation fault.

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.
 

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 3 ·
Replies
3
Views
3K
  • · Replies 59 ·
2
Replies
59
Views
12K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K