New Reply

Fortran, why do i suddenly get new errors?

 
Share Thread Thread Tools
May9-12, 09:26 AM   #1
 

Fortran, why do i suddenly get new errors?


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.
 
PhysOrg.com
PhysOrg
science news on PhysOrg.com

>> Heat-related deaths in Manhattan projected to rise
>> Dire outlook despite global warming 'pause': study
>> Sea level influenced tropical climate during the last ice age
May9-12, 12:48 PM   #2
 
Mentor
Quote by snipertje View Post
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.
 
May9-12, 12:59 PM   #3
 
Quote by Mark44 View Post
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
 
May9-12, 01:11 PM   #4

Math 2012
 
Recognitions:
Science Advisor Science Advisor

Fortran, why do i suddenly get new errors?


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).
 
May9-12, 01:26 PM   #5
 
Quote by AlephZero View Post
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.
 
New Reply
Thread Tools


Similar Threads for: Fortran, why do i suddenly get new errors?
Thread Forum Replies
Errors with fortran code, help! Programming & Comp Sci 7
What happens to a gas when it suddenly expands? Classical Physics 1
Fortran 95 Help ( Compiling errors) Programming & Comp Sci 3
errors in compling fortran Programming & Comp Sci 1
Accessing Fortran Modules within a Fortran library from Fortran Programming & Comp Sci 0