Fortran 77 - subroutine in separate file

In summary, if you want to call a subroutine from a different file, you need to compile that subroutine separately and link it in when you compile the main program.
  • #1
jf22901
55
1
Hi all

I am currently using subroutines, and placing them at the end of the main program. However, I was wondering if it is possible to save them to a separate file, which the main program then calls?

For example, in the program below I call the subroutine 'SUB', which is located at the end of the program 'TEST'. Is there any way in which it could be split so that the program 'TEST is in a file called 'test.f' and the subroutine is in a file called 'sub.f'? That way I could call the same subroutines from different programmes, rather than having to put them at the end all the time.

Many thanks,

Jack

Code:
      PROGRAM TEST
      IMPLICIT NONE
      INTEGER X, Y, Z, TOTAL, TOT
      PRINT *, 'Enter numbers X,Y,Z where Y > X'
      READ *, X, Y, Z
      CALL SUB(X, Y, Z, TOTAL)
      PRINT *, 'TOTAL =', TOTAL
      END
C***********************************************************************
C Subroutine called above is located below
C***********************************************************************
      SUBROUTINE SUB(A, B, C, TOT)
      INTEGER TOT, A, B, C, I
      TOT=C
      DO 10 I=A,B
            TOT = TOT*I
10    END DO
      RETURN
      END
 
Last edited:
Technology news on Phys.org
  • #2
I should add that to compile the above program (assuming I've called it 'test.f'), I would type at the command line:

ifort -C test.f -o test.exe
./test.exe

Jack
 
  • #3
Try this:

ifort -C test.f sub.f -o test.exe

I have gfortran, not ifort, and I don't use the -C switch. For me, this works:

gfortran test.f sub.f -o test.exe

This recompiles the subroutine every time. If you have a really big subroutine, or a collection of subroutines in a single file, that takes a long time to compile, you can compile them separately and then link them in when you compile the main program. For me it would look like this:

gfortran sub.f -c

which produces an "object file" sub.o. Note that the switch is lowercase 'c' not uppercase 'C'. Then

gfortran test.f sub.o -o test.exe
gfortran foobar.f sub.o -o foobar.exe
etc.

The .o extension signals that the file doesn't need to be compiled, just linked into the compiled program.
 
Last edited:
  • #4
Brilliant - works a treat. Thanks very much Jon.

However, I'm slightly worried that I tried it and it worked first time. That's not supposed to happen with programming is it?! :-p
 
  • #5
Hello Jack,

Yes, it is possible to save subroutines in separate files in Fortran 77. This can be achieved by using the "INCLUDE" statement in your main program. This statement allows you to include external files in your program, such as your subroutine file.

To do this, you would save your subroutine in a separate file, such as "sub.f", and then in your main program, you would use the following statement:

INCLUDE 'sub.f'

This will essentially copy and paste the contents of your subroutine file into your main program, allowing you to call the subroutine from multiple programs without having to rewrite it every time.

I hope this helps. Best of luck with your programming endeavors.

Sincerely,
 

Related to Fortran 77 - subroutine in separate file

1. What is a subroutine in Fortran 77 and why is it important?

A subroutine in Fortran 77 is a block of code that performs a specific task and can be called from within a main program. It is important because it allows for the modularization of code, making it easier to organize and maintain complex programs.

2. How do you create a subroutine in a separate file in Fortran 77?

To create a subroutine in a separate file in Fortran 77, you need to first declare the subroutine in the main program using the "EXTERNAL" statement. Then, in the separate file, you can define the subroutine using the "SUBROUTINE" statement, followed by the subroutine name and any necessary arguments.

3. Can a subroutine in Fortran 77 have multiple arguments?

Yes, a subroutine in Fortran 77 can have multiple arguments. These arguments can be of any data type and can be passed by value or by reference.

4. How do you call a subroutine from within a main program in Fortran 77?

To call a subroutine from within a main program in Fortran 77, you need to use the "CALL" statement followed by the name of the subroutine and any necessary arguments. This will transfer control to the subroutine and execute the code within it.

5. Can a subroutine in Fortran 77 modify the values of variables in the main program?

Yes, a subroutine in Fortran 77 can modify the values of variables in the main program if those variables are passed by reference as arguments. Any changes made to the variables within the subroutine will also be reflected in the main program.

Similar threads

  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
4
Views
734
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
25
Views
610
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
Back
Top