Fortran FORTRAN: Basic Question about declaring variables

  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Fortran Variables
AI Thread 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).
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
59
Views
11K
Replies
3
Views
2K
Replies
8
Views
4K
Replies
4
Views
2K
Replies
3
Views
2K
Replies
5
Views
4K
Replies
6
Views
7K
Back
Top