INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Sharing variables as an argument with Modules

Sharing variables as an argument with Modules

(OP)
Hello everyone,
I have the following problem, my main program calls subroutines that has arguments, those arguments are declared in a module but while compiling the following error appears:
Error: Symbol 'variable' at (1) has no IMPLICIT type
I dont know what is what I am missing here. I am pretty sure that is something really basic, but I'd been looking around and I cannot find the error.
I attached an example of the code that I am running. The following is the main program.

CODE -->

Program Various

Use Datos
Implicit None

call SRTN01(a,b,c)

Print *, a,b,c

End Program Various 

This is the module.

CODE --> fortran

Module Datos

Implicit None

Double Precision :: a,b,c

End Module Datos 

And finally this is the subroutine.

CODE

Subroutine SRTN01(a,b,c)

Use Datos
Implicit None

End Subroutine SRTN01 
Every code is stored in a file independently of each other. Any help would be appreciated.

RE: Sharing variables as an argument with Modules

The arguments a,b,c of the subroutine SRTN01SRTN01(a,b,c) are not the same as variables a,b,c.

If you want that your subroutine changes only the variables a,b,c in the module, then remove the subroutine arguments.

For example
srtn01.f95

CODE

Subroutine SRTN01
use datos
implicit None

write(*,*) 'hello from SRTN01'
a=10
b=20
c=30
End Subroutine SRTN01 

I named the module datos.f95 and the main program various.f95
It compiles and works now:

CODE

$ gfortran datos.f95 srtn01.f95 various.f95 -o jmauelr
$ ./jmauelr
 hello from SRTN01
   10.000000000000000        20.000000000000000        30.000000000000000 

RE: Sharing variables as an argument with Modules

I forgot to say the I modified the call in the main program too:

CODE

...
call SRTN01
... 

RE: Sharing variables as an argument with Modules

(OP)
Thanks for your response mikrom

The thing here (and that I forgot to mention) is that the real code where I'm having the problem is much larger and with many variables. I was hoping that there could be a way to declare the variables "a,b,c" globally in a Module and use that as an argument in a subroutine. There are many variables that are used commonly between many subroutines. Otherwise I will have to track down all the variables and declare them locally. If there exist another way would be great.

Actually the error is:
Error: Name 'a' at (1) is an ambiguous reference to 'a' from current program unit

Sorry I was thinking that were the same error when I was creating the small example that I posted.

I'd been trying to solve it with the COMMON statement but wit not success.

RE: Sharing variables as an argument with Modules

Quote:


I was hoping that there could be a way to declare the variables "a,b,c" globally in a Module and use that as an argument in a subroutine.
Yes you can do this with module.

Quote:


Actually the error is:
Error: Name 'a' at (1) is an ambiguous reference to 'a' from current program unit
This happens when you for example use the same name for a subroutine argument and for the global variable
For example I have this subroutine:
foo.f95

CODE

subroutine foo(a)
use datos
a=1.0
end subroutine foo 

When I try to compile this code I get the error you mentioned

CODE

$ gfortran -c foo.f95
foo.f95:3.1:

a=1.0
 1
Error: Name 'a' at (1) is an ambiguous reference to 'a' from current program unit 
It is because the compile did not know if you want to assign 1 to the global variable a found in the module datos or to the parameter a of the subroutine. The names of the subroutine parameter and the global variable collide here.

The solution is to rename parameter of the subroutine, for example:

CODE

subroutine foo(x)
use datos
a=1.0
end subroutine foo 
Then the compilation succeeds:

CODE

$ gfortran -c foo.f95 

RE: Sharing variables as an argument with Modules

Quote:


I'd been trying to solve it with the COMMON statement but wit not success.
This will work too, but the approach with modules is modern and simpler.
COMMON blocks are obsolete and error-prone.

RE: Sharing variables as an argument with Modules

Quote:


I was hoping that there could be a way to declare the variables "a,b,c" globally in a Module and use that as an argument in a subroutine.
You mix global variables and procedure arguments together. But these are different things.
If the variables are global then you can use them in your subroutine. You don't have to pass them as arguments too.

But if you need, you can use both pass some arguments to the subroutine and use global variables in it too.

for example:

CODE

Module Datos

Implicit None

Double Precision :: a,b,c

End Module Datos 

CODE

Program Various
Use Datos
Implicit None

a=1
b=2
c=3
Print *, a,b,c

call SRTN01(a+1, b+2, c+3)

Print *, a,b,c

End Program Various 

CODE

Subroutine SRTN01(x, y, z)
use datos
implicit None
Double Precision :: x, y, z

write(*,*) '-- begin from SRTN01'
write(*,*) ' incoming argument values:'
write(*,*) x, y, z
write(*,*) ' module variables values:'
write(*,*) a, b, c
write(*,*) ' ... now modifying module variables'
a=10 * x + 1
b=20 * y + 2
c=30 * z + 3
write(*,*) '-- end from SRTN01'

End Subroutine SRTN01 

If I compile and link these 3 sources and run the program I get

CODE

$ gfortran datos.f95 srtn01.f95 various.f95 -o jmanuelr

$ ./jmanuelr
   1.0000000000000000        2.0000000000000000        3.0000000000000000     
 -- begin from SRTN01
  incoming argument values:
   2.0000000000000000        4.0000000000000000        6.0000000000000000     
  module variables values:
   1.0000000000000000        2.0000000000000000        3.0000000000000000     
  ... now modifying module variables
 -- end from SRTN01
   21.000000000000000        82.000000000000000        183.00000000000000 

As you see the subroutine SRTN01 has 3 parameters x, y, z and use the 3 global variables a, b, c too.
In the main program I set the global variables:
a = 1, b = 2, c = 3
and additionally I pass to the subroutine 3 arguments
x = a+1, y = b+2, z = c+3
then the subroutine computes new values of a, b, c.

RE: Sharing variables as an argument with Modules

(OP)
Thanks again mikrom!!

Now I have a better understanding of what I can and I cannot do. I really appreciated.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close