[Fortran] Functions and naming conventions

In summary, a Fortran function can return an array, but it is not common and it is not always necessary.
  • #1
Matterwave
Science Advisor
Gold Member
3,971
328
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
  • #2
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.
 
  • #3
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?
 
  • #4
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.
 
  • #5
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...
 
  • #6
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.
 
  • #7
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.

 

Related to [Fortran] Functions and naming conventions

1. What is the purpose of using functions in Fortran?

Functions in Fortran are used to perform specific tasks or calculations and return a single result. They are useful for organizing and modularizing code, making it easier to read and maintain.

2. How are functions named in Fortran?

In Fortran, functions are typically named with a descriptive verb followed by a noun, such as "calculate_area" or "find_max_value". It is also common to use all lowercase letters and underscores to separate words in the function name.

3. Can functions have multiple return values in Fortran?

No, unlike some other programming languages, Fortran does not support multiple return values from a function. However, a function can return an array or a data structure that contains multiple values.

4. How are function arguments passed in Fortran?

In Fortran, function arguments are passed by reference, meaning that the function receives a reference to the original variable rather than a copy. This allows the function to modify the value of the argument if necessary.

5. Are there any naming conventions for function arguments in Fortran?

Yes, function arguments in Fortran are typically named with a single lowercase letter, such as "x" or "n". This convention helps to differentiate them from other variables within the function and improves code readability.

Similar threads

  • Programming and Computer Science
Replies
21
Views
2K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
12
Views
8K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
25
Views
973
  • Programming and Computer Science
2
Replies
54
Views
4K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
33
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top