FORTRAN: Basic Question about declaring variables

In summary, the conversation discusses whether it is standard to declare variables used in a subroutine in the subroutine itself or in the main code. The conclusion is that it is common and recommended to declare variables in both places, as there are two sets of variables: the actual parameters used in the main routine and the formal parameters used in the subroutine. However, if a variable is only used in subroutines and not in the main code, it may be confusing to declare it in the main code.
  • #1
Saladsamurai
3,020
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
  • #2
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
 
  • #3
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?
 
  • #4
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.
 
  • #5
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?
 
  • #6
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).
 

1. What is FORTRAN?

FORTRAN (short for Formula Translation) is a programming language used for scientific and engineering applications. It was one of the first high-level programming languages, developed in the 1950s to make it easier to write mathematical and scientific programs.

2. How do you declare variables in FORTRAN?

In FORTRAN, you declare variables by using the "DIMENSION" statement. This statement allows you to specify the name of the variable, its type (integer, real, or character), and its size. For example, to declare an integer variable named "x" with a size of 10, you would use the statement: DIMENSION x(10).

3. What are the different types of variables in FORTRAN?

FORTRAN has three main types of variables: integer, real, and character. Integer variables are used for whole numbers, real variables are used for numbers with decimal points, and character variables are used for strings of characters. There are also other specialized types such as complex numbers and logical values.

4. Can you change the value of a variable in FORTRAN?

Yes, you can change the value of a variable in FORTRAN by using an assignment statement. This involves using the equal sign (=) to assign a new value to the variable. For example, if you want to change the value of the integer variable "x" to 5, you would use the statement: x = 5.

5. Are there any rules for naming variables in FORTRAN?

Yes, there are some rules for naming variables in FORTRAN. Variable names can only contain letters, numbers, and underscores, and cannot start with a number. They also cannot be longer than 31 characters. Additionally, there are some reserved words in FORTRAN that cannot be used as variable names, such as "IF" and "DO".

Similar threads

  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
607
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
13
Views
878
  • Programming and Computer Science
Replies
3
Views
726
Back
Top