drpositron (6) [Avatar] Offline
#1
I recommend clarifying chapter 3, exercise 3. Also, it refers to the "above subroutine" as Listing 3.9, which follows and looks to be a portion of a main program. I understand what is intended, but perhaps the following is clearer:

EXERCISE 3: MODIFYING STATE WITH A SUBROUTINE

In the previous subsection, I discussed the use of intent attribute in the declaration specification of arguments. intent(in out) can be used specifically to modify a variable in-place. Can you re-write the above subroutine (Listing 3.7) such that it adds b to a, and returns a so that its value is updated in the calling program? For example, the expected behavior should be:

Listing 3.9. Invoking a subroutine that modifies an input argument in-place.

program ch3ex3

integer :: a

a = 0
call add(a, 1)
write(*,*) a ! should write "1" to the console
call add(a, 2)
write(*,*) a ! should write "3" to the console

contains
! your updated subroutine goes here
end program ch3ex3
Milan Curcic (34) [Avatar] Offline
#2
Good catch with the Listing numbering! Will be fixed.

I also like your suggestion and helps being more clear and explicit so I will adapt it for the next release.

Cheers and thank you for making this a better book!

milan