FORTRAN: Basic Question about declaring variables

  • Context: Fortran 
  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Fortran Variables
Click For Summary

Discussion Overview

The discussion revolves around the best practices for declaring variables in FORTRAN when calling subroutines from the main code. Participants explore whether variables should be declared in the main routine, in the subroutine, or both, focusing on programming standards and clarity.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant suggests that it is standard to declare variables in the subroutine itself, while another points out that variables can be declared in both the main routine and the subroutine.
  • Another participant emphasizes the distinction between actual parameters in the main routine and formal parameters in the subroutine.
  • A later reply questions the necessity of declaring a variable in the main routine if it is only used in subroutines, suggesting it may be confusing.
  • One participant clarifies that variables must be included in the call to the subroutine, implying that they should be declared in the main routine if they are used there.

Areas of Agreement / Disagreement

Participants express differing opinions on the best practice for variable declaration, with no consensus reached on whether it is better to declare variables in the main routine, the subroutine, or both.

Contextual Notes

Some participants note that the choice of where to declare variables may depend on their usage context, but this remains an unresolved aspect of the discussion.

Saladsamurai
Messages
3,009
Reaction score
7
If you call a subroutine from the MAIN code, is it standard to declare the variables used in the subroutine in the subroutine itself or in the main code? My gut tells me the former, but the horrendously written code before me tells me the latter.

Any thoughts?
 
Technology news on Phys.org
Guess what? Variables can be declared in both the main routine and the subroutine!

Check it:

Program Test

Real x, y, ans

Call Calc (x, y, ans)

End
C
Subroutine Calc (A, B, C)

Real A, B, C

C = A + B

RETURN
END
 
SteamKing said:
Guess what? Variables can be declared in both the main routine and the subroutine!

Check it:

Program Test

Real x, y, ans

Call Calc (x, y, ans)

End
C
Subroutine Calc (A, B, C)

Real A, B, C

C = A + B

RETURN
END

Hi SteamKing :smile:

I guess my question is more along the lines of "what is better programming" and not so much which one works. Is it common to declare them in the MAIN and not in the sub?
 
Variables should be declared in both places. There really are two sets of variables: the actual parameters that are used in your main routine, and the formal parameters that are used in your subroutine.
 
Mark44 said:
Variables should be declared in both places. There really are two sets of variables: the actual parameters that are used in your main routine, and the formal parameters that are used in your subroutine.

Ok. But if the variable is not used in Main at all and is only used in various sub routines, then isn't weird to declare it in Main? If not 'weird' ... confusing?
 
You're using a variable when you call the subroutine, as in call Calc(x, y, ans) -- SteamKing's example.

I'm assuming that you have variables in the call to the subroutine or function, rather than literals (i.e., constants of some kind).
 

Similar threads

  • · Replies 59 ·
2
Replies
59
Views
12K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 6 ·
Replies
6
Views
7K