Topic: Sharing variables as an argument with Modules

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.

 Program Various

Use Datos
Implicit None

call SRTN01(a,b,c)

Print *, a,b,c

End Program Various 

This is the module.

 Module Datos

Implicit None

Double Precision :: a,b,c

End Module Datos 

And finally this is the subroutine.

 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

Your final subroutine is the problem:

 Subroutine SRTN01(a,b,c)

Use Datos
Implicit None

End Subroutine SRTN01

The arguments to the subroutine are local variables.  The module variables a, b, and c can't be used as your argument definitions.

In this case, I'm not sure why you'd pass module variables at all since you're wanting to access them in the subroutine as well.  The code should just be:

 Program Various

Use Datos
Implicit None

call SRTN01()

Print *, a,b,c

End Program Various 

and

 Subroutine SRTN01()

Use Datos
Implicit None

End Subroutine SRTN01 

There's no reason to pass the variables as arguments.

Jeff Armstrong
Approximatrix, LLC

Re: Sharing variables as an argument with Modules

More likely what you want to do is leave out the USE statement in the subroutine so that this routine can act on the dummy arguments a, b, and c.

Subroutine SRTN01(a,b,c)

Implicit None

End Subroutine SRTN01

Re: Sharing variables as an argument with Modules

Thank you Jeff and Baf1 for your response.

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

Yes, the error you got is what I was expecting, "ambiguous reference".  This is gfortran's way of saying, "hey, you have declared a variable as local (dummy argument) with the same name as a imported module variable; an impossible situation!".  The compiler doesn't know what to make of that situation, as you are trying to use the same name for two different variables. 

If you really want the module variables to be available to the subroutine (same rules for a function), then just USE the module  and remove those variables from the argument list.  However, if the actual arguments (the ones in the call statement) vary from one call to the next, then remove the USE statement and keep the arguments. 

You  might want to do some reading in a Fortran book or online tutorial, as this a feature of Fortran, and has nothing to do with Simply Fortran.

Re: Sharing variables as an argument with Modules

If what you want to achieve is to avoid typing, you can use an INCLUDE file.

! Include file defs.inc
double precision :: a, b, c
! Main program
Program Various
Implicit None
include 'defs.inc'
call SRTN01()
Print *, a,b,c
End Program Various
Subroutine SRTN01()
Implicit None
include 'defs.inc'
End Subroutine SRTN01

It doesn't gain you much in this simple example. If you have so many arguments that you need such tricks to maintain the code, perhaps it is time to restructure it.

--
David