The things which define the type parameters and array bounds of objects are known as specification expressions. In the code of the question we see two such specification expressions with real(dp) :: v(9)
: the kind type parameter dp
and the array bound 9
.
For a specification expression, only its value matters. Although we don't recommend such things, the primacy of value is why real(kind=33)
can be used consistently throughout a program. So, if you want to declare two objects to be the same kind, you just have to use expressions which evaluate the same:
real(kind=33) x1, f1
real(kind=selected_real_kind(15)) x2, f2
x1=f1()
x2=f2()
end
function f1()
real(kind=33) f1
f1=1
end function f1
function f2()
real(kind=selected_real_kind(15)) f2
f2=2
end function
Above are shown two styles of specification expressions: using a literal constant (33
) and appropriately referencing an intrinsic function (selected_real_kind(15)
). But specification expressions can also use objects accessible in the scope. We have several techniques for "sharing" objects between scopes, and we can use these techniques in our specification expressions.
Indeed, the declarations in
function vector_norm(n,vec) result(norm)
integer, intent(in) :: n
real(dp), intent(in) :: vec(n)
show one such way with name association! The dummy argument n
is used in the specification of vec
's array bound. This n
is argument associated with the actual argument 9
of the main program: we've used a value in the main program to tell the function something about one of its arguments.
Alas, argument association isn't a form of name association which is useful for kind type parameters, because kind type parameters must be constant (which dummy arguments aren't). We can use a dummy argument to tell us the length of a character object or size of an array, but not the character's kind.
We can use other forms of name association to share constants between one scope (such as main program) and another (a function).
We can use host association or use association. I won't recreate code examples from each of those answers, but hopefully this answer motivates why those two approaches work. Neither approach is necessarily better than the other, but once you understand what they aim to do with regards to scope you can compare suitability on other aspects.
Note that the function vector_norm
of the first linked answer is itself using both use and host association for the kind parameter (and argument association for the array bound).