- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Please help me understand this:
If F1 and F2 are Subroutines, then the program compiles and runs just fine. But if F1 and F2 are Functions, then the program gives a compilation error: Undefined symbols: "_F2_", referenced from: _MAIN__
In the latter case, what do I need to do to make the compiler "see" F2?
Here are example programs that display the above behavior:
!-------------------------
program test1
real :: x,a1
x=5.
call f1(x,a1)
print*, a1
contains
subroutine f1(x,a1)
real, intent(in) :: x
real, intent(out) :: a1
call f2(x,a1)
end subroutine f1
subroutine f2(x,a1)
real, intent(in) :: x
real, intent(out) :: a1
a1=2.*x
end subroutine f2
end program test1
!-------------------------
program test2
real :: x,a1
x=5.
a1=f1(x)
print*, a1
contains
function f1(x)
real :: f1,f2
real, intent(in) :: x
f1=f2(x)
end function f1
function f2(x)
real :: f2
real, intent(in) :: x
f2=2.*x
end function f2
end program test2
!-------------------------
1 Solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In the second program, the one which uses functions f1 and f2, replace
real:: f1,f2
by
real :: f1
and run the program. Then, read the manuals on the topic of "host-association".
What happens is that in the body of internal function f1(x), the declaration of f2 as real overwrites the host-association of f2. Since f2 is already available and its attributes are known to the compiler by host-association, a redeclaration of f2 as real tells the compiler to override the previous instance of f2.
real:: f1,f2
by
real :: f1
and run the program. Then, read the manuals on the topic of "host-association".
What happens is that in the body of internal function f1(x), the declaration of f2 as real overwrites the host-association of f2. Since f2 is already available and its attributes are known to the compiler by host-association, a redeclaration of f2 as real tells the compiler to override the previous instance of f2.
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Interestingly your both versions of the same code work fine with gfortran. Also if we write the second code like below, we get the right result by ifort too.
[fortran]program test2 real :: x,a1 real, external :: f1 x=5. a1=f1(x) print*, a1 end program test2 function f1(x) real :: f1,f2 real, intent(in) :: x f1=f2(x) end function f1 function f2(x) real :: f2 real, intent(in) :: x f2=2.*x end function f2[/fortran]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In the second program, the one which uses functions f1 and f2, replace
real:: f1,f2
by
real :: f1
and run the program. Then, read the manuals on the topic of "host-association".
What happens is that in the body of internal function f1(x), the declaration of f2 as real overwrites the host-association of f2. Since f2 is already available and its attributes are known to the compiler by host-association, a redeclaration of f2 as real tells the compiler to override the previous instance of f2.
real:: f1,f2
by
real :: f1
and run the program. Then, read the manuals on the topic of "host-association".
What happens is that in the body of internal function f1(x), the declaration of f2 as real overwrites the host-association of f2. Since f2 is already available and its attributes are known to the compiler by host-association, a redeclaration of f2 as real tells the compiler to override the previous instance of f2.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you! So obvious, after your explanation.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page