# Result attribute

**URL:** <https://fortran-lang.discourse.group/t/result-attribute/5207>\
**Category:** Homework\
**Created:** [February 14, 2023, 4:53pm UTC](https://fortran-lang.discourse.group/t/result-attribute/5207 "2023-02-14T16:53:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [February 14, 2023, 4:53pm UTC](https://fortran-lang.discourse.group/t/result-attribute/5207/1 "2023-02-14T16:53:39Z")

</div>

This post seeks confirmation if I understood the `result attribute` well enough.

Milan Curci’s [Modern Fortran](https://www.manning.com/books/modern-fortran) introduces the `result attribute` by comparison of

```f90
! the following is listing 3.5:
integer function sum(a, b)
  integer, intent(in) :: a,b
  sum = a + b
end function sum  

```

with

```f90
! the following is listing 3.6:
integer function sum(a, b) result(res)
  integer, intent(in) :: a,b
  res = a + b
end function sum  

```

including the statement

> The advantage to using the result attribute may not be obvious from this example [listing 3.6 vs. listing 3.5, ndlr] because the name of the function (`sum`) is already quite short, …

In context with earlier listings in chapter 03 _Reusable code with functions and subroutines_, I would paraphrase the function of listing 3.6 as:

> The inclusion of either definition 3.5, or 3.6 causes the main program to use `sum` as defined by this (local / user defined) function; because _for the main program_, the local definition in its very file file is of higher hierarchy than Fortran’s intrinsic `sum` function. However – contrasting to 3.5 – with the later listing 3.6, _within the user defined function_, the sum is computed as `res`, i.e. now equally independent from Fortran’s intrinsic `sum` function, too.

My question hence: is paraphrasing listing 3.6 _this way_ an acceptable one?

---

<div class="post-metadata">

**Author:** ![milancurcic](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/milancurcic/32/2_2.png) [@milancurcic](https://fortran-lang.discourse.group/u/milancurcic)\
**Post date:** [February 14, 2023, 5:27pm UTC](https://fortran-lang.discourse.group/t/result-attribute/5207/2 "2023-02-14T17:27:13Z")

</div>

> [@nbehrnd](#):
>
> However – contrasting to 3.5 – with the later listing 3.6, _within the user defined function_, the sum is computed as `res`, i.e. now equally independent from Fortran’s intrinsic `sum` function, too.

I had to try this to find out and it seems that the answer is no–even inside the function definition from listing 3.6, invoking `sum` would mean invoking the external function recursively, and not the intrinsic one.

It’s unfortunate, and I regret it, that I shadowed the intrinsic `sum` for this minimal example. It’s possible that it created more confusion than clarity.

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [February 14, 2023, 5:34pm UTC](https://fortran-lang.discourse.group/t/result-attribute/5207/3 "2023-02-14T17:34:53Z")

</div>

@nbehrnd ,

FYI, see [**this**](https://fortran-lang.discourse.group/t/is-it-possible-to-overwrite-intrinsics/3654/22).

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [February 14, 2023, 6:01pm UTC](https://fortran-lang.discourse.group/t/result-attribute/5207/4 "2023-02-14T18:01:39Z")

</div>

If the question is whether the `result()` clause allows one to use the intrinsic `sum()` within the user function `sum()`, then the answer is no. Not even if you declare `intrinsic :: sum` within the function, you still cannot do that. That is because any references to `sum()` within the function are assumed to be recursive references to itself.

That is the main purpose of the `result()` clause, its allows a function to directly reference itself in expressions within the body of the function.

---

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [February 14, 2023, 8:03pm UTC](https://fortran-lang.discourse.group/t/result-attribute/5207/5 "2023-02-14T20:03:50Z")

</div>

Then I have to re-read the text up to listing 3.6 because when writing my question here, I did not consider recursion (as e.g., in the example in the [Fortran wiki](https://fortranwiki.org/fortran/show/recursion) as related to the `result attribute`):

```plaintext
 program ackermann
   integer :: ack
   write(*,*) ack(3, 12)
 end program ackermann

 recursive function ack(m, n) result(a)
   integer, intent(in) :: m,n
   integer :: a
   if (m == 0) then
     a=n+1
   else if (n == 0) then
     a=ack(m-1,1)
   else
     a=ack(m-1, ack(m, n-1))
   end if
 end function ack

```

Then, `result attribute` were like an anchor (or seed?) to start/initiate the computation of Fibonacci, or of factorial `n!`, which (less Fortran idiomatic) possibly could be defined differently, too; perhaps vaguely similar to the array computations split into a) `dh(1) = h(1) - h(grid_size)` for one element alone, followed by b) a block

```plaintext
do concurrent (i = 2:grid_size)
  dh(i) = h(i) - h(i-1)
end do

```

for the every subsequent element to consider.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [February 14, 2023, 8:23pm UTC](https://fortran-lang.discourse.group/t/result-attribute/5207/6 "2023-02-14T20:23:14Z")

</div>

If one wants to call the function recursively, `result(...)` must be used. Otherwise, the name of the result variable shadows the name of the function, and one cannot refer to the function inside itself.

---

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [February 14, 2023, 8:43pm UTC](https://fortran-lang.discourse.group/t/result-attribute/5207/7 "2023-02-14T20:43:36Z")

</div>

@FortranFan The example you point to provides an glimpse of what is possible. As such, it goes _considerably_ beyond Milan’s book about _Writing procedures that operate both on scalars and arrays_ (chapter 3) with a bridge like

```plaintext
program subroutine_example
  implicit none

  print *, sum(3, 5)
  print *, sum([1, 2],3)
  print *, sum(1, [2, 3, 4])
  print *, sum([1, 2, 3], [2, 3, 4])

contains

pure elemental integer function sum(a, b)
  integer, intent(in) :: a, b
  sum = a + b
end function sum  

end program subroutine_example

```

because your example requires solid insight where/how Fortran’s implementation of `sin` can/could/should be improved .plus. the mathematics. Hence for me, this taps more into the field of Fortran _development_ rather than _grateful use_ of what generations of computer scientists and committee members identified as «good, we use this implementation».

---

<div class="post-metadata">

**Author:** ![nbehrnd](https://avatars.discourse-cdn.com/v4/letter/n/e0b2c6/32.png) [@nbehrnd](https://fortran-lang.discourse.group/u/nbehrnd)\
**Post date:** [February 14, 2023, 8:55pm UTC](https://fortran-lang.discourse.group/t/result-attribute/5207/8 "2023-02-14T20:55:09Z")

</div>

> [@everythingfunctional](#):
>
> Otherwise, the name of the result variable shadows the name of the function, and one cannot refer to the function inside itself.

This becomes evident e.g., in manual (pen and paper) calculation of factorials. I get this point, it is a prerequisite to access the next round of the algorithm.

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [February 14, 2023, 10:48pm UTC](https://fortran-lang.discourse.group/t/result-attribute/5207/9 "2023-02-14T22:48:46Z")

</div>

> [@nbehrnd](#):
>
> The example you point to provides an glimpse of what is possible. As such, it goes _considerably_ beyond Milan’s book about _Writing procedures that operate both on scalars and arrays_ (chapter 3) with a bridge like  
> …  
> because your example requires solid insight where/how Fortran’s implementation of `sin` can/could/should be improved .plus. the mathematics. Hence for me, this taps more into the field of Fortran _development_ rather than _grateful use_ of what generations of computer scientists and committee members identified as «good, we use this implementation»

No idea what you mean by your last sentence, “'this taps more into the field of Fortran _development_ rather than _grateful use_ of what generations of computer scientists and committee members identified as «good, we use this implementation»”

As to the rest of your comments here, you now appear to convey with a statement such as “Writing procedures that operate both on scalars and arrays” that your interest is in `ELEMENTAL` procedures whereas with your original post it appeared you were interested in something else viz. " The inclusion of either definition 3.5, or 3.6 causes the main program to use `sum` as defined by this (local / user defined) function; because _for the main program_ , the local definition in its very file file is of higher hierarchy than Fortran’s intrinsic `sum` function"

As to my example, you can try to **not** read much into the mathematics or Fortran implementations of the trigonometric function but simply note by controlling the subscope via a `BLOCK` construct or some such mechanism and also the `intrinsic` attribute, you can flirt with danger or confusion to a casual reader of the program and direct the main program to invoke an override of an intrinsic or the intrinsic function itself. Another silly example along somewhat similar lines, here indulging in (mis)use of generic interface and resolution, is:

```fortran
module m
   interface sum
      module procedure my_sum
   end interface
contains
   elemental integer function my_sum(a, b)
      integer, intent(in) :: a, b
      my_sum = sum( [a, b] ) !<-- this is for illustration purposes only; don't think it's any wise!
   end function
end module
   use m
   print *, sum([1,2],[3,4])
end 

```
