0

I'm writing a simple program for the squared module of a vector, could you help me?

I don't find anything useful for a beginner in fortran 2008, especially in these simple operations between arrays.

function squared_module(n)
integer :: module2
integer, dimension(3), intent(in) :: n
module2=n(1)**2+n(2)**2+n(3)**2
end function

program propagator

implicit none

integer, dimension(3) :: m
integer :: module2

write (*,*) "Give me a vector"
read (*,*) m

module2=squared_module(m)

print*, "Module squared is ", module2

end program propagator

ERRORS I OBTAIN:

propagator.f08:38:26:

 module2=squared_module(m)
                      1

Error: Return type mismatch of function ‘squared_module’ at (1) (UNKNOWN/REAL(4)) propagator.f08:38:12:

 module2=squared_module(m)
        1

Error: Function ‘squared_module’ at (1) has no IMPLICIT type

Nico Battelli
  • 61
  • 1
  • 2
  • 7
  • Note that your function `squared_module` could be replaced by a call to the intrinsic routine `dot_product`, eg `dot_product(m,m)`. – High Performance Mark Sep 04 '19 at 07:08
  • You have two problems here: 1) the function hasn't been declared needs to be (because of `implicit none`) - you can use an explicit (better) or implicit interface to do that; 2) you aren't correctly defining the function result (you have a local variable `module2` you are defining rather than the correct function result `squared_module`. Those duplicate questions address each point between them. – francescalus Sep 04 '19 at 08:45

1 Answers1

1

You have two choices. You can add module2 as a result variable in your function declaration.

function squared_module(n) result(module2)

You can rewrite the function as

function squared_module(n)
   integer :: squared_module
   integer, dimension(3), intent(in) :: n
   squared_module=n(1)**2+n(2)**2+n(3)**2
end function
steve
  • 657
  • 1
  • 4
  • 7
  • This is correct, but doesn't address the main point of the question: that there is no declaration of the function `squared_module` in the main program. That the function result hasn't been defined correctly is relevant, but not the main point. – francescalus Sep 04 '19 at 08:46
  • @francescalus what I could modify? I'm a beginner, can you write it correctly for me? Thank you! – Nico Battelli Sep 04 '19 at 11:50
  • @fransecalus, yep, I missed the `implicit` declaration error. OP can solve that by adding `integer, external :: squared_module` to the main program. – steve Sep 04 '19 at 16:50