Fortran: Problems with dummy variables in functions

In summary, the conversation is about a function that is not working properly with arrays as dummy variables. The solution was to turn the function into a subroutine, as it is supposedly better at dealing with arrays. However, one person suggests that there is no difference between subroutines and functions, except for the return value.
  • #1
thefury
7
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
  • #2
You need to inform the compiler that the formal parameters x, y, and z are arrays rather than scalar values.
 
  • #3
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.
 
  • #4
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.
 
  • #5


It appears that there may be an issue with the way the function is being called in the program. It is important to note that in Fortran, dummy variables are used to pass values from the calling program to the function. Therefore, it is crucial to ensure that the correct variables are being passed in the correct order. In this case, it seems that the first dummy variable (x) is not being assigned the correct value (b), resulting in a segmentation fault when it is used in operations within the function.

One possible solution could be to check the code in the calling program and make sure that the correct values are being passed to the function in the correct order. It may also be helpful to use explicit variable names in the function call to avoid any confusion.

Additionally, it is important to consider the data types of the variables being passed to the function. In this case, the variables are arrays with integer elements, so it is important to make sure that the function is able to handle these types of variables properly.

In conclusion, it is important to carefully check the code in both the calling program and the function to ensure that the correct values are being passed and that the function is able to handle the data types of the variables being passed to it. With some careful debugging and attention to detail, the issue with the dummy variables should be able to be resolved.
 

Related to Fortran: Problems with dummy variables in functions

1. What are dummy variables in Fortran?

Dummy variables in Fortran are variables that are used as placeholders in a function or subroutine. They are temporary variables that are used to pass values into the function or subroutine and are discarded once the function or subroutine is executed.

2. Why do I encounter problems with dummy variables in functions?

Problems with dummy variables in functions can occur due to a variety of reasons. Some common issues include using incorrect data types, not properly initializing the variables, or not passing the correct number of arguments into the function.

3. How can I troubleshoot problems with dummy variables in functions?

To troubleshoot problems with dummy variables in functions, you can start by carefully checking the data types of the variables and ensuring they are compatible with the function. You should also check if you have properly initialized the variables and if you are passing the correct arguments into the function.

4. Can I use arrays as dummy variables in Fortran functions?

Yes, you can use arrays as dummy variables in Fortran functions. However, you need to make sure that the array dimensions and data types match the corresponding arguments in the function declaration.

5. Is there a limit to the number of dummy variables I can use in a Fortran function?

Yes, there is a limit to the number of dummy variables you can use in a Fortran function. This limit varies depending on the compiler being used, but it is typically in the range of a few hundred to a few thousand variables.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
3
Views
825
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
12
Views
2K
Back
Top