Fortran Fortran function calling with wrong arguments

  • Thread starter Thread starter harish88
  • Start date Start date
  • Tags Tags
    Fortran Function
AI Thread Summary
The discussion centers on a Fortran function, vdot, which is designed to accept arrays but is called with a scalar input, raising questions about potential errors. The function operates correctly because the scalar input is actually a specific element from an array, allowing the function to treat it as an array starting from that position. The distinction between passing an entire array versus a single element is crucial, as the function interprets the input based on its definition and the context of the call. It is emphasized that careful attention is needed to ensure the function accesses the correct section of the array. Overall, the behavior of the function is consistent with Fortran's handling of assumed-size arrays and specific array elements.
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
 
Technology 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
20
Views
3K
Replies
3
Views
2K
Replies
22
Views
5K
Replies
8
Views
2K
Back
Top