Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Fortran] Functions and naming conventions

  1. Sep 22, 2014 #1

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    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 (Text):

      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 (Text):

    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
     
  2. jcsd
  3. Sep 22, 2014 #2

    Mark44

    Staff: Mentor

    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.
     
  4. Sep 22, 2014 #3

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    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?
     
  5. Sep 22, 2014 #4

    Mark44

    Staff: Mentor

    You shouldn't need a comment to explain what the name of a function means.
    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.
     
  6. Sep 22, 2014 #5

    Matterwave

    User Avatar
    Science Advisor
    Gold Member

    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 have turned them all into subroutines so hopefully that will work...
     
  7. Sep 22, 2014 #6

    rcgldr

    User Avatar
    Homework Helper

    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.
     
  8. Sep 24, 2014 #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.

     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [Fortran] Functions and naming conventions
  1. Fortran - Functions (Replies: 0)

Loading...