Fortran, why do i suddenly get new errors?

In summary, The conversation discusses the difference between subroutines and functions in Fortran and how they handle returning values. The participants also discuss the use of a do-loop to test a variable for different values, with one of them having difficulty getting the desired result. It is ultimately determined that the subroutine should be called within the loop to properly calculate the value of y.
  • #1
snipertje
11
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
  • #2


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.
 
  • #3


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:
  • #4
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).
 
  • #5
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.
 

1. Why am I suddenly getting syntax errors when running my Fortran code?

There could be several reasons for this. One possibility is that you have made a mistake in your code, such as forgetting to close a parenthesis or using an incorrect variable name. Another possibility is that you are using an outdated version of Fortran that does not support certain syntax. It's also possible that your code is not compatible with the compiler you are using. Check your code for errors and make sure you are using the correct version of Fortran and compatible compiler.

2. How do I fix "undefined reference" errors in my Fortran code?

"Undefined reference" errors typically occur when your code is trying to call a function or subroutine that has not been defined. Check your code to make sure all functions and subroutines are properly defined and that you are using the correct syntax to call them. If the error persists, it could also be caused by a missing library or module. Make sure all necessary libraries and modules are included in your program.

3. My Fortran code was working before, but now it won't compile. What could be causing this?

This could be due to changes in your operating system or updates to your compiler. It's also possible that a recent change in your code has caused an error. Check for any recent updates or changes that could be causing the issue. Additionally, make sure your code is compatible with the current version of your compiler and make any necessary adjustments.

4. How can I debug my Fortran code to find and fix errors?

There are several tools and techniques you can use to debug your Fortran code. One option is to use a debugger specifically designed for Fortran, such as GDB or TotalView. These tools allow you to step through your code and track the values of variables to find where the error is occurring. You can also use print statements or write to a file to track the values of variables and see where your code may be going wrong. It's also important to thoroughly test your code and check for any errors before running it.

5. I'm getting "out of memory" errors when running my Fortran program. How can I fix this?

"Out of memory" errors occur when your program is trying to use more memory than is available. This could be due to a large amount of data or an inefficient use of memory in your code. One way to address this is to optimize your code to reduce the amount of memory it requires. You can also try increasing the amount of memory available to your program or running it on a machine with more memory. Additionally, make sure you are properly deallocating any memory that is no longer needed in your code.

Similar threads

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