Fortran, why do i suddenly get new errors?

  • Context: Fortran 
  • Thread starter Thread starter snipertje
  • Start date Start date
  • Tags Tags
    Errors Fortran
Click For Summary

Discussion Overview

The discussion revolves around the behavior of subroutines in Fortran, specifically regarding how they return values and how to properly implement them in a loop to compute results for different input values. Participants explore the differences between subroutines and functions, and how to structure their code to achieve the desired outcomes.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant questions whether subroutines return values, noting that their subroutine only produces a single output when called outside a loop.
  • Another participant clarifies that Fortran subroutines do not return values and suggests using a function instead, which does return a value.
  • A participant shares their code and expresses confusion about why their results are not as expected, indicating that they are calling the subroutine only once outside of the loop.
  • Suggestions are made to call the subroutine within the loop to ensure it updates the output for each iteration.
  • There is a discussion about the necessity of the variable y, with some suggesting it may not be needed at all.
  • One participant acknowledges the explanation and expresses understanding of how the subroutine should be structured to recalculate y for each new value of t.

Areas of Agreement / Disagreement

Participants generally agree on the need to call the subroutine within the loop to achieve the desired output. However, there is some disagreement regarding the necessity of using a separate variable for the output, as well as the distinction between subroutines and functions.

Contextual Notes

There are unresolved aspects regarding the handling of variable types, particularly the interaction between real and integer types in function calls, which may affect the behavior of the code.

snipertje
Messages
11
Reaction score
0
Do subroutines return values only?

Sorry for the wrong title!

I currently have a subroutine which should return y like (the actual formula is a bit more complex): y=cos(t)

When i call the subroutine and then make a do-loop to test y for different t, it gives 1 value only. Does the subroutine give a value to y or am I doing something else wrong?

t doesn't have a value before the subroutine is called.
 
Last edited:
Technology news on Phys.org


snipertje said:
Sorry for the wrong title!

I currently have a subroutine which should return y like (the actual formula is a bit more complex): y=cos(t)

When i call the subroutine and then make a do-loop to test y for different t, it gives 1 value only. Does the subroutine give a value to y or am I doing something else wrong?

t doesn't have a value before the subroutine is called.
Fortran subroutines don't return values. By that, I mean that an expression that includes a subroutine call doesn't evaluate to anything.

Your routine should be a FUNCTION, which does return a value.
 


Mark44 said:
Fortran subroutines don't return values. By that, I mean that an expression that includes a subroutine call doesn't evaluate to anything.

Your routine should be a FUNCTION, which does return a value.
I have something like this, it's not about wether or not a subroutine is needed, I want to understand how it works.

Code:
call asd(y,t,a)
do i=1,10
t=i
test(i)=y
end do

subroutine asd(y,t,a)
y=a*cos(t)
end
For some reason test(i) gives the same value for every i, whereas the following works as i want it to, note that t doesn't have a value before the subroutine call.
Code:
do i=1,10
t=i
test(i)=a*cos(t)
end do
 
Last edited:
If your first code you only call the subroutine once. You need to call it inside the loop like this.
Code:
do i=1,10
    t=i
    call asd(y,t,a)
    test(i)=y
end do
You don't really need the variable y. You could do
Code:
do i=1,10
    t=i
    call asd(test(i),t,a)
end do
(Note, you DO need t, because by default t is a real variable and i is an integer, so
call asd(y,i,a) won't work. (It will probably compile, but give you the wrong answers).
 
AlephZero said:
If your first code you only call the subroutine once. You need to call it inside the loop like this.
Code:
do i=1,10
    t=i
    call asd(y,t,a)
    test(i)=y
end do
You don't really need the variable y. You could do
Code:
do i=1,10
    t=i
    call asd(test(i),t,a)
end do
(Note, you DO need t, because by default t is a real variable and i is an integer, so
call asd(y,i,a) won't work. (It will probably compile, but give you the wrong answers).
Ok, thanks I think I get it now. As soon as y has a value it's no longer a*cos(t), so by taking the subroutine in the loop, y get's redeclared as a*cos(t) every single loop, after which y can be calculated with a new t again.
 

Similar threads

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