Fortran function calling with wrong arguments

  • Context: Fortran 
  • Thread starter Thread starter harish88
  • Start date Start date
  • Tags Tags
    Fortran Function
Click For Summary

Discussion Overview

The discussion revolves around the behavior of a Fortran function, specifically the function `vdot`, which is defined to accept arrays as inputs but is called with a scalar value in one instance. Participants explore whether this should result in an error and clarify the implications of Fortran's handling of array arguments.

Discussion Character

  • Technical explanation, Conceptual clarification, Debate/contested

Main Points Raised

  • One participant questions why the function `vdot`, which is defined to accept arrays, works when called with a scalar input, suggesting it should throw an error.
  • Another participant points out that without additional context, it is unclear whether `w` and `v(kp)` are arrays or scalars, indicating a need for more information.
  • A different participant asserts that while `w` is indeed an array, `v(kp)` represents a specific value at the index `kp`, thus the function is called with an array and a scalar.
  • Another participant explains the concept of "assumed-size arrays" in Fortran, detailing how the function interprets the arguments based on their types and positions, and emphasizes the importance of ensuring the function accesses the correct section of the array.
  • A later reply acknowledges the explanation provided, indicating understanding of the situation.

Areas of Agreement / Disagreement

Participants express differing views on the nature of the arguments passed to the function and whether the behavior observed is expected or problematic. The discussion does not reach a consensus on the implications of calling the function with a scalar.

Contextual Notes

There is a lack of clarity regarding the definitions and types of the variables involved, particularly `w` and `v(kp)`, which affects the understanding of the function's behavior.

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.
 

Similar threads

Replies
31
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 22 ·
Replies
22
Views
5K
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K