How to Convert Fortran to Visual Basic
By Samuel Porter
Fortran was the world's first procedural programming language, and it brought with it a whole host of improvements to the art and science of programming. It made it easier than ever before to translate mathematical ideas into machine language. However, since its release in 1957, many other language have come to the fore, and it has remained in use only for limited purposes. Visual Basic is a programming language that is pervasive on Microsoft's operating system and programs, so integrating Visual Basic code is often much easier than integrating Fortran code. As a result, you may find it useful to translate old Fortran programs into Visual Basic to promote future maintainability.
Open Microsoft Visual Basic .NET and create a new Command Line project. A window should appear showing the Main subroutine of the program.
Open the Fortran source code. Begin by translating the subroutines in the Fortran program. Fortran subroutines look like this:
SUBROUTINE mySubroutine(a, b, c) REAL :: a, b, c END SUBROUTINE
The same subroutine in Visual Basic would look like this:
Sub mySubroutine(a As Double, b As Double, c As Double) End Sub
As you can see, the beginning and ending code does not significantly change, but REAL becomes Double and is applied to each argument individually, instead of all of them at once.
Convert the functions in your program. In Fortran, a function looks like this:
INTEGER FUNCTION plus(a, b) INTEGER :: a, b plus = a + b END FUNCTION plus
The same function in VB.NET would look like this:
Function plus(a As Integer, b As Integer) As Integer Return a + b End Function
The return type, which is expressed before the FUNCTION keyword in Fortran comes at the end of the Function line in Visual Basic, and the argument types move from inside the function body to the Function line (just like with subroutines). The return value, which is expressed in Fortran as an assignment statement (using '=') whose left-hand value is the name of the function itself, is expressed in VB.NET using the Return statement (without any equals sign).
Translate any library functions used in the Fortran code into their equivalents in Visual Basic. Both Fortran and VB come with extensive function libraries. You can look up Visual Basic library functions by going to the Microsoft Developer Network's Visual Basic reference at http://msdn.microsoft.com/en-us/library/sh9ywfdk.aspx
You may find that some of the library functions do not have direct equivalents--every language has a different set of strengths and weaknesses. If this occurs, you will need to write your own Visual Basic function to reproduce the behavior of the Fortran function. To make sure you understand and are accurately reproducing the Fortran function's behavior, make sure you refer to the Fortran documentation. You can download Intel's Fortran documentation at http://www.intel.com/software/products/compilers/techtopics/for_prg.htm
When you've finished converting your program, make sure to test it against the Fortran program. Run the Fortran and Visual Basic programs on the same input, and ensure that they produce the same result. Test on as many different inputs as you can think of to ensure that the converted program has remained faithful to the old program.
References
Warnings
- Converting programs from one language to another is a difficult, time-consuming and error-prone process. Make sure you keep the old code at least until you have extensively tested the new code, so that, if there are inconsistencies, you will be able to refer to it to fix the error.
Writer Bio
Samuel Porter has degrees in computer science and law. Before joining Demand Studios, he often wrote technical documents as a part of his software consulting work and contributed to community forums covering a wide variety of technical topics.