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

Fortran function calling with wrong arguments

  1. Aug 10, 2011 #1
    I have a program I am trying to understand. According to the function definition(vdot), the function acccepts arrays as inputs. But the function is called with a scalar input and it works just fine. Shouldnt it throw an error ?

    w and v are arrays of different sizes. ms is a scalar constant.

    Code (Text):
          real*8 function vdot(x,y,size)
          real*8 x(*),y(*)
          integer size,j
          vdot = 0.d0
          do j = 1,size
            vdot = vdot + x(j)*y(j)
          end do
    Calling it as

    Code (Text):
                do k = 1,i
                kp = (k-1)*ms+1
                h(k,i) = vdot(w,v(kp),ms)
  2. jcsd
  3. Aug 10, 2011 #2


    Staff: Mentor

    How do you know that w and v(kp) in the call to vdot aren't arrays? You haven't provided enough context for anyone to tell what these two variables represent.
  4. Aug 10, 2011 #3
    While w is an array, I believe v(kp) will be a particular value at the kp index location, isnt it ? So we are essentially calling the function with an array w and a scalar value from a specific location in the v array.
  5. Aug 11, 2011 #4


    User Avatar

    Staff: Mentor

    In the function, the dummy arguments x and y are declared as arrays of indefinite size (the Fortran terminology is "assumed-size array"). The dummy argument "size" determines the effective actual size of the arrays, as far as the calculations are concerned.

    In the function call, when the actual argument is just an array name, the corresponding dummy argument in the function is an array that starts at the beginning of the actual array. When the actual argument is a specific element of an array, the corresponding dummy argument is an array that starts at that position in the actual array. In effect, the function "sees" only the section of the actual array that begins at the specified position. You need to be very careful when playing "tricks" like this, of course, to make sure that the function actually uses the correct section of the actual array.

    In your example, if kp = 5, then y(1) in the function corresponds to v(5) in the main program (or calling function), y(2) corresponds to v(6), etc.
  6. Aug 11, 2011 #5
    Thank you. That's clearly what is happening there.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook