Fortran function calling with wrong arguments

  • Context: Fortran 
  • Thread starter Thread starter harish88
  • Start date Start date
  • Tags Tags
    Fortran Function
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
4 replies · 4K views
harish88
Messages
3
Reaction score
0
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:
      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
      end

Calling it as

Code:
            do k = 1,i
            kp = (k-1)*ms+1
            h(k,i) = vdot(w,v(kp),ms)
            enddo
 
Physics news on Phys.org
harish88 said:
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:
      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
      end

Calling it as

Code:
            do k = 1,i
            kp = (k-1)*ms+1
            h(k,i) = vdot(w,v(kp),ms)
            enddo

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.
 
While w is an array, I believe v(kp) will be a particular value at the kp index location, isn't it ? So we are essentially calling the function with an array w and a scalar value from a specific location in the v array.
 
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.
 
Thank you. That's clearly what is happening there.