- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi all,
I know this topic/ error was discussed early many times, but i could not find solution to my program. Here is a simple program that emits an error, 'The same named entity from different........'. Not using module works fine, i mean declaring all the variables each time in main and sub programs, but when i use module to declare global variables it gives me error.I amtrying not to use common blocks, but use module so thatI can beadaptive to the changes in the language. I do not understand why it was being a ambiguous reference.
My compiler version is 11.1.035, and i tried turning off Generrate interface blocks and Check routine interfaces, but no help. I would appreciate any helpful advice.
I know this topic/ error was discussed early many times, but i could not find solution to my program. Here is a simple program that emits an error, 'The same named entity from different........'. Not using module works fine, i mean declaring all the variables each time in main and sub programs, but when i use module to declare global variables it gives me error.I amtrying not to use common blocks, but use module so thatI can beadaptive to the changes in the language. I do not understand why it was being a ambiguous reference.
My compiler version is 11.1.035, and i tried turning off Generrate interface blocks and Check routine interfaces, but no help. I would appreciate any helpful advice.
[cpp] module global integer :: i REAL ::lambda1,lam1,lambda2,speed1,speed2 real ,parameter :: vis=6.5e-9,width=0.2968,p0=15,c=0.0005 end module global PROGRAM main use global print*, 'Enter 1 for lambda and 2 for speed' read*, i if(i.eq.1) then call speed(lam1) else call lambda(speed1) endif END PROGRAM main subroutine speed(lam1) use global write(*,*) 'Enter the speed' read(*,*) speed2 lam1=(6*VIS*SPEED2*WIDTH)/(P0*(C**2)) print*, lam1 end subroutine speed subroutine lambda(speed1) use global write (*,*) 'Enter Lambda' read(*,*) lambda2 speed1= (lambda2*p0*c**2)/(6*vis*width) print*, 'speed=',speed1 end subroutine lambda [/cpp]
Link Copied
5 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Module global declares a variable named lam1 and subroutine speed declares a dummy argument named lam1 - this causes a conflict, since how is the compiler to know which you want? Change one name or the other or resolve the issue some other way. (If you are not interested in the lam1 from the module, you can use ONLY or a rename clause on the USE.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - Steve Lionel (Intel)
Module global declares a variable named lam1 and subroutine speed declares a dummy argument named lam1 - this causes a conflict, since how is the compiler to know which you want? Change one name or the other or resolve the issue some other way. (If you are not interested in the lam1 from the module, you can use ONLY or a rename clause on the USE.)
Thanks Steve.
Does it mean dummy arguments need not to be declared as variables , especially when usingMODULE ? So in the module global, I don't declare dummy variables (dummy variables and actual arguments being same), then my code compiles ! But how does the compiler know what type of variable the dummy and actual argument are? This is little bit incomprehensible. Below is the modified code. Please take a look at it and provide any suggestions.
[cpp] module global implicit none real :: lambda2,speed2 real ,parameter :: vis = 6.5e-9,width = 0.2968,p0 = 15,c = 0.0005 end module global PROGRAM main use global call option(i) if(i.eq.1) then call speed(lam1) else call lambda(speed1) endif END PROGRAM main subroutine option(i) use global print*, 'Enter 1 for lambda and 2 for speed' read*, i end subroutine subroutine speed(lam1) use global write(*,*) 'Enter the speed' read(*,*) speed2 lam1=(6*vis*speed2*width)/(p0*(c**2)) print*, lam1 end subroutine speed subroutine lambda(speed1) use global write (*,*) 'Enter Lambda' read(*,*) lambda2 speed1= (lambda2*p0*c**2)/(6*vis*width) print*, 'speed=',speed1 end subroutine lambda [/cpp]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You misunderstood my reply. The dummy argument lam1 and the module variable lam1 are different entities. Yes, you should always declare dummy arguments and you have to do that in the routine. But if you choose a dummy argument with the same name as a name that comes from a module you USE, you have to resolve the conflict because Fortran does not "hide" module variables this way.
I see in your revised version you removed the declarations from the module. Ok - now add the dummy argument declarations to the routines as appropriate.
I see in your revised version you removed the declarations from the module. Ok - now add the dummy argument declarations to the routines as appropriate.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - Steve Lionel (Intel)
You misunderstood my reply. The dummy argument lam1 and the module variable lam1 are different entities. Yes, you should always declare dummy arguments and you have to do that in the routine. But if you choose a dummy argument with the same name as a name that comes from a module you USE, you have to resolve the conflict because Fortran does not "hide" module variables this way.
I see in your revised version you removed the declarations from the module. Ok - now add the dummy argument declarations to the routines as appropriate.
I see in your revised version you removed the declarations from the module. Ok - now add the dummy argument declarations to the routines as appropriate.
Steve,
I thought the variable in module global and dummy argument are the same entitites, I understood that module is to declare global variables and once Ithrow all the variables in module and 'use' it in the routines,it will be nicer and easier, that I don't have to declare any variablesin the subroutines, and again in the main program.
You know what I am saying. So if i am dealing with routines, i have to declare the arguments (dummy or actual) in the routines as well as the main program that I am referencing the routines . Isn't it easier if there is someway to declare all the arguments in module and just use USE statement?
Thanks a lotfor your insight !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Peter,
When you declare a variable in a module, it is a new, global variable. Think of it like COMMON. It is not simply a template for declarations that would otherwise be required in local routines. Dummy arguments are local entities in routines and need to be declared there - there is no shortcut to that.
You are not declaring variables "again" - in your initial code, you declared two different variables that had the same name. Even though you didn't have a declaration statement for lam1 in the subroutine, it was implicitly declared because of its appearance in the dummy argument list.
Actual arguments are things in a call or function reference - they should be declared wherever is appropriate. If you want them to be global variables, you can declare them in the module, or locally in the routine.
Think carefully before declaring module variables - they are global, static storage that can interfere with later attempts to build multithreaded applications.
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