program main
real, parameter :: a = 1
!real :: a
!a=1
res = func(a)
write(*,*) res
end program main
function func(a)
real, parameter :: b=a+1 !(*)
func = b
return
end function func
My compiler complains at the line marked (*). Is there a way to set the value of a constant with a value coming outside that function?
You can't declare "b" as a parameter since its value is not constant at compile time, since it depends on the function argument.
It is a good idea to use "implicit none" so that you are sure to declare all variables. Also to place your procedures into a module and "use" that module so that the interface is known to the caller. As in:
module my_funcs
implicit none
contains
function func(a)
real :: func
real, intent (in) :: a
real :: b
b = a + 1
func = b
return
end function func
end module my_funcs
program main
use my_funcs
implicit none
real, parameter :: a = 1
real :: res
res = func(a)
write(*,*) res
end program main
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With