[Fortran] Functions and naming conventions

  • Context: Fortran 
  • Thread starter Thread starter Matterwave
  • Start date Start date
  • Tags Tags
    Fortran Functions
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
6 replies · 2K views
Messages
4,099
Reaction score
427
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
 
Physics 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.