(Please, check the content section if interested in Fortran-related topics and maybe subscribe to the newsletter ;-) ) A Fortran main program is contained in between the instructions "program ... end program", according the general scheme: program ProgramName implicit none [variable declaration] [main body] end program ProgramName In principle, we do not need to introduce external procedures called by the main program in order to write a working Fortran code. However, external procedures become highly useful when the main body of the program is made up of different huge subsections. Thus, the idea is to construct a tool, say external_procedure(variable1, variable2,...,variableN), able to recall in the main program a series of complicated instructions written elsewhere. Such technique brings with it many pros:
In Fortran there are three types of external procedures
I code f(x) using a Fortran function. When I need to code entire subprograms, in that cases, I personally use subroutines. The correct way to define functions and subroutines can be found here, where some fully coded examples are discussed. You will also find an example on how to use modules, which, in any case, will be discussed soon on my blog as well ( stay tuned :-) ). Where should I put external procedures and How do I call them?Typically external procedures are written before the instruction program ProgramName or after the instruction end program ProgramName. They might be eventually called by the main program or any other external procedure. For example, let us suppose that we are writing the external procedures after the instruction end program ProgramName and we have an external function FuncExtProcedure(Var1,Var2,Var3) and an external subroutine SubExtProcedure(Var1,Var2,Var3) . Our code will look like the following one program ProgramName implicit none real*8 FuncExtProcedure ! <-- LOOK: HERE I'M DECLARING THE FUNCTION NAME. NO NEED TO ! DECLARE THE SUBROUTINE. IF YOU ARE NOT CALLING THE ! FUNCTION IN THE MAIN PROGRAM, NO NEED TO DECLARE IT real*8 Var1, Var2, Var3,y [declaration of other variables] y = FuncExtProcedure(Var1,Var2,Var3) !function call call SubExtProcedure(Var1,Var2,Var3) !subroutine call [main body] end program ProgramName function FuncExtProcedure(Var1, Var2, Var3) implicit none real*8 FuncExtProcedure ! <-- LOOK: HERE I'M DECLARING THE FUNCTION NAME. real*8 Var1, Var2, Var3 [declaration of other (DUMMY) variables] [function body] !I MIGHT WANT TO CALL THE SUBROUTINE SubExtProcedure IN THIS FUNCTION, IN SUCH !CASE I JUST CALL IT EXACTLY LIKE IN THE MAIN PROGRAM, I.E. call SubExtProcedure(Var1,Var2,Var3) !subroutine call FuncExtProcedure = [SPECIFY THE FINAL VAULE OF YOUR FUNCTION] return end function FuncExtProcedure subroutine SubExtProcedure(Var1, Var2, Var3) implicit none real*8, intent(in):: Var1 real*8, intent(inout):: Var2 real*8, intent(out):: Var3 real*8 FuncExtProcedure ! <-- LOOK: IF I NEED TO CALL THE ABOVE FUNCTION IN THIS !SUBROUTINE, I MUST DECLARE IT. [declaration of other (DUMMY) variables] [subroutine body] return end subroutine SubExtProcedure The instruction intent(in):: Var1 tells to the compiler that Var1 is a variable passed by the main program (or any oder external procedure) to the subroutine. The value of VAR1 cannot be changed by the subroutine. If you attempt to change such value in the main body of the subroutine, you'll get an error message from the compiler!!!!! The instruction intent(inout):: Var2 tells to the compiler that Var2 is a variable which can be either passed by the main program (or any oder external procedure) or returned by the subroutine as an output of the operations in the main body of the subroutine. The difference with Var1 is that if you pass the initial value of Var2 to the subroutine, that value could be eventually overwritten by the subroutine, if in the main body of the subroutine there's an instruction like Var2 = functionof(Var2), without getting any error from the compiler when you compile. Finally, the instruction intent(out):: Var3 tells to the compiler that Var3 is a variable whose value (the value of the variable and not the variable itself) is not passed by the main program (or any oder external procedure) to the subroutine, however, it is returned by the subroutine to the main program (or any oder external procedure). This means that Var3 will be evaluated by the subroutine main body using the input variable(s) Var1 or (eventually) the input/output variable(s) Var2. Here we go. Now let's call the above code as Myfile.f and let's compile it with the instruction gfortran -O3 Myfile.f -o out.x once the output out.x is ready, we can run it using the instruction ./out.x and that's it: have fun!!!
0 Comments
![]() This Picture by www.giuseppeforte.me is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
(Please, check the content section if interested in Fortran-related topics and maybe subscribe to the newsletter ;-) )
Hey there! This is Giuseppe. In this post I am going to give you some advices on how to use the basic Input/Output (I/O) operations in Fortran. Stay tuned if you are interested in other programming languages, because I just started my journey in Scientific Communication. Why communicating programming languages to the general public? The reply to this question is actually easy. The first step towards a deep Scientific development cannot put the coding abilities away. Nowadays, coding means having a trusty tool to better understand Scientific results. That said, coding must be considered as something to bring to the general attention of the public. In my posts, you will find arguments related to Fortran, Matlab, Mathematica, R, C++ and much more...just give me the time. Divulgative posts on Science will also be discussed here...again: give me the time :-) So....let's start! Using a high level programming language requires experience. However, some basic facts can be actually learned relatively fast. Such basic syntactic structures represent the foundations of any code, regardless the particular application. Specific applications in Scientific programming (and also non scientific programming) require necessarily the knowledge of the most common Input/Output (I/O) operations: tell to your machine something, allow the machine to communicate you the result and...hope that your machine will not realize that it can solve problems faster and better than you :-) (that would make the machine equivalent to Skynet :-) ). In this post, I am going to tell you about the basic I/O operations, using a high level programming language known as Fortran. Before starting working with Fortran or any other programming language, you might want to know how to edit, compile and run a Fortran code. Some info about all these processes are deeply described here. In general, like any other programming language, the I/O operations you are allowed to perform using Fortran, could be summarized in four steps
Please, feel free to leave here any kind of comment. I will be really glad to reply to all your questions. The material about the I/O operations can be found here. Have fun !!!!! :-) |
AuthorPersonal blog: http://www.giuseppeforte.me/ Archives |