Fortran Fortran, why do i suddenly get new errors?

  • Thread starter Thread starter snipertje
  • Start date Start date
  • Tags Tags
    Errors Fortran
AI Thread Summary
Subroutines in Fortran do not return values; instead, they modify variables passed to them. In the discussed scenario, the user attempted to use a subroutine to compute values of y based on varying t, but only received a single value because the subroutine was called outside of the loop. To achieve the desired functionality, the subroutine must be called within the loop for each iteration, allowing y to be recalculated with the updated value of t. It was clarified that using a function would be more appropriate if a return value is needed. The final understanding was that y must be recalculated in each loop iteration to reflect the new value of t.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top