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).
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

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
3K
Replies
6
Views
7K
Back
Top