Fortran FORTRAN: Basic Question about declaring variables

  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Fortran Variables
Click For Summary
When calling a subroutine from the main code, it is standard practice to declare variables within both the main routine and the subroutine. This approach allows for clear differentiation between actual parameters used in the main routine and formal parameters defined in the subroutine. While variables can be declared in either location, declaring them in both places enhances code clarity and maintainability. If a variable is exclusively used within a subroutine, declaring it in the main code may seem unnecessary or confusing, especially if it is not referenced elsewhere. However, it is important to ensure that the variables passed to the subroutine are not literals but rather variable references, which facilitates proper data handling within the subroutine.
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).
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 59 ·
2
Replies
59
Views
11K
  • · 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