Silverfrost Forums

Welcome to our forums

Problems with function / implicit type

6 Dec 2013 11:53 #13407

I have a problem with a function and its data type. Here is a very basic code (which will be expanded after I solved my problem):

module params
real,parameter :: spin = 0.5;
real,parameter :: gvalue = 2;
real,parameter :: exchange = 10; !cm^-1
integer,parameter :: length = 2;
integer,parameter :: hamsize = int((2*spin+1)**length)
end module params

program matrix
use params
implicit none

integer,dimension(length) :: vec;

vec(:) = create_base_vector(1);

write(*,*) vec;


end program matrix

function create_base_vector(pos)
use params
implicit none
integer :: pos;
integer,dimension(length) :: create_base_vector;

create_base_vector(1) = 2;
create_base_vector(2) = 3;

end function create_base_vector

The function create_base_vector should create an array. But if I want to compile it, the compiler tells me 'Error: Function 'create_base_vector' at (1) has no implicit type'. I tried to remove implicit none, but than the compiler tells me 'Error: Function 'create_base_vector' is of rank 1 at (1) and of rank 0 at (2)', where (1) is the function definition and (2) is the function call.

Does anybody know how to solve this problem?

6 Dec 2013 12:09 #13408

The function needs to be part of the module.

Like this:

module params
real,parameter :: spin = 0.5;
real,parameter :: gvalue = 2;
real,parameter :: exchange = 10; !cm^-1
integer,parameter :: length = 2;
integer,parameter :: hamsize = 4 ! int((2*spin+1)**length)

contains

    function create_base_vector(pos)
    implicit none
    integer :: pos;
    integer,dimension(length) :: create_base_vector;

    create_base_vector(1) = 2;
    create_base_vector(2) = 3;

    end function create_base_vector

end module params

program matrix
use params
implicit none

integer,dimension(length) :: vec;

vec(:) = create_base_vector(1);

write(*,*) vec;


end program matrix

I changed your declaration of hamsize. Unfortunately you can't do it the way you had it in F95.

6 Dec 2013 12:22 #13409

Thank you very much, that did work. The declaration of the hamsize constant works with g95 compiler very well. Do I really have to change this? When the programm is finished, I want to use different values of length or spin, so the way I do it now is more comfortable.

6 Dec 2013 4:20 #13410

It works in g95 because it is a F2003 feature which that compiler supports.

If you can use integers then this should work.

(I assume spin X 2 is always an integer).

integer, parameter :: two_spin = 1,  length = 2
real, parameter :: spin = two_spin/2.0
integer, parameter :: hamsize = two_spin**length
Please login to reply.