How do i 'relabel' variables (in fortran)

In summary, pointers can be used in Fortran 90 and newer, but they are more restrictive than in other languages like C/C++. To use a global variable in a formula without cluttering the code, you can declare it as a constant and use a pointer to reference it in the formula. This is possible in Fortran by using the "target" and "pointer" keywords.
  • #1
tothetop
2
0
Suppose I have a global variable like "GauntCoefficients".. and I want to use it in a formula in the next block of code. In C++, I might do something like this to make the code more readable:

const int* gc = GauntCoefficients;
(then write some nasty formula with "gc" rather than "GauntCoefficients" everywhere)

Since "gc" is declared here as a constant, the compiler should just delete it (I think). And since "gc" id declared locally.. it doesn't pollute the namespace.

Can I do something like that in Fortran?
 
Technology news on Phys.org
  • #2
Hey
In Fortran 90 and newer pointers are possible to use. But they are more restrictive than in e.g. C/C++.
You can therefore do something like this:
Code:
integer, target::GauntCoefficients
integer, pointer::gc
gc=>GauntCoefficients

I hope this helps.
 
  • #3


In Fortran, you can use the "parameter" statement to declare a constant variable that can be used in place of the original variable. For example:

parameter :: gc = GauntCoefficients

This will allow you to use "gc" in place of "GauntCoefficients" in your formula without changing the original variable. Additionally, since "gc" is declared as a constant, it will not be changed during the execution of the program.

You can also use the "use" statement to access global variables from a different module without having to use the full variable name. For example:

use mymodule, only: GauntCoefficients

This will allow you to use "GauntCoefficients" in your formula without having to specify the module name every time.

In summary, there are ways to "relabel" variables in Fortran for the purpose of improving readability and avoiding namespace pollution. The "parameter" and "use" statements are two options that can achieve this.
 

Related to How do i 'relabel' variables (in fortran)

1. How do I relabel variables in Fortran?

To relabel variables in Fortran, you can use the RENAME statement. This statement allows you to assign a new name to a variable or a set of variables. For example, you can use the statement RENAME (OLDVAR = NEWVAR) to rename a single variable or RENAME ((OLDVAR1, OLDVAR2) = (NEWVAR1, NEWVAR2)) to rename multiple variables at once.

2. Can I change the data type of a variable using the relabeling method?

No, relabeling only changes the name of a variable. If you want to change the data type of a variable, you will need to use the appropriate type conversion functions or declarations in Fortran.

3. Is it possible to relabel variables within a loop or subroutine?

Yes, you can use the RENAME statement within a loop or subroutine to rename variables. However, keep in mind that the new names will only be valid within the specific scope of the loop or subroutine.

4. What happens if I try to relabel a variable that does not exist?

If you try to relabel a variable that does not exist, you will receive an error message. Make sure to double-check the spelling and syntax of your RENAME statement to avoid any errors.

5. Can I undo a variable relabeling in Fortran?

No, once a variable has been relabeled, the original name cannot be retrieved. It is important to carefully consider your variable names and their relevance before using the RENAME statement.

Similar threads

  • Programming and Computer Science
Replies
25
Views
863
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
3
Views
871
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
13
Views
955
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
3
Views
827
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
13
Views
2K
Back
Top