forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problems with function / implicit type

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
plutz



Joined: 06 Dec 2013
Posts: 2

PostPosted: Fri Dec 06, 2013 12:53 pm    Post subject: Problems with function / implicit type Reply with quote

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):

Code:
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?
Back to top
View user's profile Send private message
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Fri Dec 06, 2013 1:09 pm    Post subject: Reply with quote

The function needs to be part of the module.

Like this:

Code:

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.
_________________
Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl
Back to top
View user's profile Send private message
plutz



Joined: 06 Dec 2013
Posts: 2

PostPosted: Fri Dec 06, 2013 1:22 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Fri Dec 06, 2013 5:20 pm    Post subject: Reply with quote

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).

Code:

integer, parameter :: two_spin = 1,  length = 2
real, parameter :: spin = two_spin/2.0
integer, parameter :: hamsize = two_spin**length

_________________
Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group