[Fortran] Functions and naming conventions

  • Context: Fortran 
  • Thread starter Thread starter Matterwave
  • Start date Start date
  • Tags Tags
    Fortran Functions
Click For Summary

Discussion Overview

The discussion revolves around naming conventions and the functionality of functions in Fortran, particularly in the context of defining and using Hamiltonian matrices. Participants explore the implications of naming functions and variables the same, as well as the capability of functions to return arrays.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • One participant expresses uncertainty about whether naming a function and a variable the same (both as hspinm) will cause confusion for the compiler.
  • Another participant argues that using the same name for a function and a variable is a bad practice, suggesting that distinct names improve code readability.
  • There is a discussion about the meaning of the name hspinm, with one participant explaining it stands for "h_spin part_majorana" and acknowledging the need for clearer naming.
  • Questions arise regarding whether a function in Fortran can return an array, with some participants suggesting that subroutines might be a better alternative for this purpose.
  • One participant mentions that Fortran functions can return arrays but requires further study of the language, particularly regarding modules and interfaces.
  • Another participant notes that while Fortran can return arrays, it may be more complex than earlier versions of the language.

Areas of Agreement / Disagreement

Participants generally agree that naming functions and variables the same is problematic, but there is no consensus on the best naming conventions or the specifics of returning arrays from functions.

Contextual Notes

There are unresolved questions about the capabilities of Fortran functions regarding array returns and the implications of using subroutines instead. The discussion reflects varying levels of familiarity with Fortran's features and best practices.

Matterwave
Science Advisor
Homework Helper
Gold Member
Messages
3,971
Reaction score
329
Hi guys,

In the code I'm working on there are a few functions that will be used to find a Hamiltonian, which I will then use in later parts of the code to find the time evolution of my system. Right now I have it set up so that the function names are actually the names I want to give the Hamiltonian matrix.

So, for example, I have something like:

Code:
  function hspinm
    real(kind=reel8),dimension(nflavor,nflavor) :: hspinm   
   end function hspinm

Right now, I don't know how to calculate this Hamiltonian so it is an empty function, but it should return an nflavor by nflavor matrix.

Now, if in my main code I declare an array and also call it hspinm and write something like

Code:
program bulb
real(kind=reel8),dimension(nflavor,nflavor)::hspinm
hspinm=hspinm(arguments)
end program bulb

function hspinm
    real(kind=reel8),dimension(nflavor,nflavor) :: hspinm   
end function hspinm

Will this work? I'm guessing I should name it something different? What I mean with this piece of code is the matrix hspinm should be filled with the elements after calling the function hspinm. Will the compiler get confused on the names, or will the presence of arguments inside the second hspinm statement make it understand that I am calling the function?

Thanks
 
Technology news on Phys.org
Regardless of whether the compiler permits such ambiguity as you propose, it's a bad idea to have one name represent an array, the name of a function, and the value returned by the function. I have done too little Fortran programming recently to know if the language is sophisticated enough to have separate namespaces for functions vs. variables. Have some pity for anyone reading your code, including yourself a few weeks or months after you've written it, and give these things distinct names.

Also, hspinm is not in my opinion a very useful name. Apparently 'h' represents Hamilitonian, and 'm' stands in for matrix. How much effort does it take to name a variable so that its purpose is immediately obvious? You don't have to go crazy, but the days are long past when variable names were limited to eight characters.
 
It stands for h_spin part_majorana

I used a comment to explain its intention. I will rename the functions, thanks.

In that case, I must ask, can a function return an array? I'm not very good at programming for sure, so there's a lot of basic questions I don't really know. Perhaps I should make them subroutines?
 
Matterwave said:
It stands for h_spin part_majorana

I used a comment to explain its intention.
You shouldn't need a comment to explain what the name of a function means.
Matterwave said:
I will rename the functions, thanks.

In that case, I must ask, can a function return an array? I'm not very good at programming for sure, so there's a lot of basic questions I don't really know. Perhaps I should make them subroutines?
I don't believe a Fortran function can return an array. I could be wrong, though. A subroutine can have array parameters, and can change them, so in essence, a subroutine can "return" an array.
 
Mark44 said:
You shouldn't need a comment to explain what the name of a function means.
If I named the objects with the correctly designated name, they would be really long. H_spincoherence_majorana_offdiagonalonly, H_spincoherence_Dirac_sterile_lefthanded, etc. Maybe I will think of better names. I will keep this advice in mind.

I don't believe a Fortran function can return an array. I could be wrong, though. A subroutine can have array parameters, and can change them, so in essence, a subroutine can "return" an array.

I have turned them all into subroutines so hopefully that will work...
 
Matterwave said:
In that case, I must ask, can a function return an array?
Fortran function parameters are passed by reference (by address), so a "returned" array could be passed as an input parameter. A function could also allocate an array and return it as a result, but I don't know how common this is with Fortran.
 
Yes, it is a very bad idea to name your functions and variables the same.

For Fortran or any other language, you may want to consult naming conventions...google it, there is a good set of rules out there; in particular, I seem to recall such conventions while studying Java and Python.

As far as returning an array in Fortran, it is possible; but, you need to study a bit more of Fortran. Fortran used to be rather simple; with the advent of Fortran 90, it got more versatile, more powerful, but also a bit more complicated.

Sure, Fortran can return arrays; but it can also handle arrays a-la-matlab and it can have something as powerful as array-valued functions. One thing you need to read up on is "modules"...they are going to make your life a lot easier. I would say you also need to read up on "interfaces"; but, if you use modules, chances are you can get away without explicit interfaces for the most part, as the module will issue most of them for you behind the scenes.

 

Similar threads

  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
Replies
12
Views
9K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 54 ·
2
Replies
54
Views
5K
  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 33 ·
2
Replies
33
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K