Fortran function calling with wrong arguments

In summary: The function is being called with w(5), v(5), and ms(1), which are the values of w, v, and ms in that order.In summary, the function works just fine with a scalar input, but should throw an error if it were called with an array.
  • #1
harish88
3
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
Thank you. That's clearly what is happening there.
 

1. What is Fortran function calling?

Fortran function calling is the process of executing a specific function in a Fortran program. Functions are subprograms that perform a specific task and can be called multiple times within a program.

2. What happens if I call a Fortran function with the wrong arguments?

If a Fortran function is called with the wrong arguments, the program will likely produce unexpected results or even crash. This is because the function is expecting specific data types and formats for its arguments, and if these are not provided correctly, the function will not be able to execute properly.

3. How can I avoid calling a Fortran function with the wrong arguments?

To avoid calling a Fortran function with the wrong arguments, it is important to carefully read the function's documentation and understand the data types and formats that are expected for each argument. Additionally, using proper error handling techniques in your code can help catch and prevent incorrect function calls.

4. Can using the wrong arguments in a Fortran function lead to errors in my entire program?

Yes, using the wrong arguments in a Fortran function can lead to errors in your entire program. This is because functions are often used multiple times throughout a program, and if one function is not called correctly, it can affect the results of other functions or the overall program execution.

5. How can I fix a Fortran function that is called with the wrong arguments?

To fix a Fortran function that is called with the wrong arguments, you will need to go back and identify where the incorrect arguments are being passed. This may involve reviewing the function's documentation, checking the data types and formats of the arguments being passed, and making necessary changes to ensure the correct arguments are used.

Similar threads

  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
4
Views
601
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
936
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
4
Views
7K
Back
Top