3

I'm somewhat new to Fortran, but I'm starting to collect a lot of functions and subroutines in modules that I use over and over again. I've been trying to build my own library so that I could call some of these functions and subroutines from new source code that I write. So far I've been able to get the subroutines to work, but not the functions. I just don't know how to call the function from with in the code.

Here's an example. The following function takes in an integer and returns the Identity matrix.

module test
contains
function IDENTITY(rows) !RETURNS THE IDENTITY MATRIX                                 
    real, dimension(rows,rows)    :: IDENTITY                                        
    integer, intent(in)           :: rows                                      
    integer                       :: i, j                                                                  
    !f2py intent(in) rows                                                             
    !f2py intent(out) IDENTITY                                                        
    !f2py depend(rows) IDENTITY                                                       
    IDENTITY = ZEROS(rows,rows)                                                      
    do i = 1, rows                                                                   
        do j = 1, rows                                                               
            if (i == j) then                                                         
               IDENTITY(i,j) = 1                                                     
            endif                                                                    
        enddo                                                                        
    enddo                                                                            
end function IDENTITY
end module                       

Now I compile this into an object file then archive it into a library. I then write a small program in which I'd like to use this function--and here's the problem I want to avoid. I've figured I have to put a \use\ statement in my source so that it will use the module. Then I've got to include the path to the .mod when compiling. But eventually, I'm going to have a whole section full of use statements. I'd like to avoid having to do all those things and just make everything nice and neat. Is there a way? Any help would be great,

Thank You

1 Answer 1

4

You can put many subroutines and functions into the contains section of one module. You don't need a separate module for each. It makes sense to organize your code by creating modules/files for related procedures (subroutines and functions).

Libraries are a good strategy that I used to use. In this era I usually don't bother with the library. Compilers are so fast that I just compile the files with the modules needed by the program. It depends on how big your code is.

You invoke a subroutine with a call statement. You invoke a function by using it in an expression such as an assigment statement:

x = sin (y)
matrix = identity (n)

P.S. Instead of:

do i = 1, rows
   do j = 1, rows
      if (i == j) then
         IDENTITY(i,j) = 1
      endif
   enddo
enddo

why not:

do i = 1, rows
   IDENTITY(i,i) = 1
enddo

Do you have a function ZEROS to zero your matrix? The language will do that with an assigment statement:

IDENTITY = 0.0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.