What is Subroutine: Definition and 36 Discussions

In computer programming, a subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed.
Subroutines may be defined within programs, or separately in libraries that can be used by many programs. In different programming languages, a subroutine may be called a routine, subprogram, function, method, or procedure. Technically, these terms all have different definitions. The generic, umbrella term callable unit is sometimes used.The name subprogram suggests a subroutine behaves in much the same way as a computer program that is used as one step in a larger program or another subprogram. A subroutine is often coded so that it can be started several times and from several places during one execution of the program, including from other subroutines, and then branch back (return) to the next instruction after the call, once the subroutine's task is done. The idea of a subroutine was initially conceived by John Mauchly during his work on ENIAC, and recorded in a January 1947 Harvard symposium on "Preparation of Problems for EDVAC-type Machines". Maurice Wilkes, David Wheeler, and Stanley Gill are generally credited with the formal invention of this concept, which they termed a closed subroutine, contrasted with an open subroutine or macro. However, Turing had discussed subroutines in a paper of 1945 on design proposals for the NPL ACE, going so far as to invent the concept of a return address stack.Subroutines are a powerful programming tool, and the syntax of many programming languages includes support for writing and using them. Judicious use of subroutines (for example, through the structured programming approach) will often substantially reduce the cost of developing and maintaining a large program, while increasing its quality and reliability. Subroutines, often collected into libraries, are an important mechanism for sharing and trading software. The discipline of object-oriented programming is based on objects and methods (which are subroutines attached to these objects or object classes).
In the compiling method called threaded code, the executable program is basically a sequence of subroutine calls.

View More On Wikipedia.org
  1. tysonman166

    Troubleshooting MCNP6: 'Bad Trouble in Subroutine Source' & More

    Hi! so i kinda stuck when i tried to run my code in MCNP6 because the output keep showing me "bad trouble in subroutine source of mcrun you need a source subroutine." While I am sure i already put my KCODE and KSRC in my code (on the picture below). Could anyone help...
  2. P

    Comp Sci Can‘t call a subroutine name in a subroutine in Fortran90

    Here is the code for example: PROGRAM main CALL B() CALL CaoB(B) contains subroutine B() IMPLICIT NONE write (*,*) 'fk' end subroutine B subroutine CaoB(fcn) implicit none INTERFACE SUBROUTINE fcn() IMPLICIT NONE END SUBROUTINE fcn END INTERFACE write...
  3. J

    Fortran Fortran subroutine solving for the velocity and displacement of a spring

    I have a simple Fortran code for solving velocity and displacement of spring, the code works fine when writing all together without using a subroutine. but as I am learning Fortran I tried to do it using subroutine, but I keep getting errors appreciate any help in advance. program msd implicit...
  4. vjvaibhu

    Fortran Calling a subroutine in Fortran 77

    I am calling a fortran 77 subroutine in a fortran 90 code and the results i am getting are wrong as confirmed from matlab. Can anybody please let me know where is the problem? do isss=1,5 do ig=1,10 dat0=dat(:,ig,isss) do iss=1,5 if...
  5. V

    Fortran FUNCTION attribute conflicts with SUBROUTINE attribute

    Hello, I'm trying to use Runge Kutta with adaptive stepsize from numerical recipes(page 742, https://websites.pmc.ucsc.edu/~fnimmo/eart290c_17/NumericalRecipesinF77.pdf ). I rewrote code but I have got failure messages both in an attachment. If I cut this subroutine everything is fine also this...
  6. D

    Fortran File not written after subroutine call

    I'm testing a part of a code and I have a problem. After the subroutine call at line 44 (CALL ZEIGSUB), nothing is written in UNIT NRES. The other units are written correctly. Here's the code: PROGRAM TEST IMPLICIT NONE complex *16, allocatable :: KSTAR(:,:), VR(:,:), CM2(:,:) real *8...
  7. E

    Fortran How does Fortran know the Inputs and Outputs of Subroutines

    I have trouble understanding how Fortran knows the inputs and outputs of a subroutine. I am studying a code written in Fortran, but I do not have a compiler, so I cannot check how it works. Here is my problem; for example, I have a subroutine, subroutine test(x, y, z, a, b, c) The variables...
  8. Y

    F90 Subroutine that accepts any array of any dimension

    Hi, I need to write a subroutine that accepts as an argument an array of any number of dimensions, where each dimensions has any size. The array is contiguously allocated. In C, I can do this pretty cleanly. void array_func(int ndims, int *dims, int *array) { // do stuff with...
  9. K

    Fortran [Fortran 77] Subroutine for computing a global spherical har

    Greetings, I want to ask if there is any subroutine for computing a global spherical harmonic reference field. I read journal and they say it exists, I hope we can share information regarding this subject. Thank you in advance.
  10. H

    Fortran Fortran - passing unknown array to subroutine

    Im trying to pass an unknown size of array to the subroutine. Then I will define the size of array in subroutine and pass it back to main program. I tried to use interface but the complier said invalid memory reference or Explicit interface required for ' ' at (1): allocatable argument . Any...
  11. Somefantastik

    Fortran Clear a variable in fortran subroutine

    Hello, I am trying to figure out how to clear a variable inside a subroutine from a calling function. The variable in the subroutine acts as a persistent(MATLAB) or static(C), but once in so many calls I want it to be reset to empty as if the subroutine is being called for the first time. I...
  12. S

    C/C++ Anologous to subroutine in C++

    Hello, I am relevant new user to C++ while i am quit comfortable using fortran! One of my favourite thing to use in fortran was the subroutines but i struggling to find something similar in C++. Let me give you an example: In Fortan: subroutine test(Tabular1,Tabular2) ... ...
  13. Hepth

    Fortran Fortran 77 [F77] : Code in external file without subroutine?

    I have some F77 code and I am wondering if it is possible to inject external code into a separate file and call it without it being a subroutine, and placed into the code. An example would be : a = 1 + 1 b = a + 1 c = b + a a = c + b d = 4 + a and I want to take lines 3 and 4 and...
  14. S

    Mcnp source subroutine linking trouble

    Hi, I have written a source subroutine and I am trying to link it with mcnp6. When I run my input file it says you need a source subroutine. My input file and the source subroutine(written in fortran) are in the same directory. Where do I have to keep the subroutine to link with mcnp? Any help...
  15. K

    Fortran [Fortran] subroutine needs fixing

    I am trying to use this subroutine which I found online in my model but I am not experienced in programming and I need to know how can I check for errors or debug the code and try to find the mistakes or syntax errors , I have attached the code for your review Thank you
  16. S

    Fortran Help FFT subroutine fortran 77

    Does someone have an effective two-dimensional FFT subroutine in Fortran 77?
  17. S

    Fortran [Fortran] Problem with subroutine

    Hello everyone ; I have tried to run a program which has many subrotines so I control each of them.But, when I run one of them, the below error message is seen; /tmp/ccMqKR9L.o: In function `MAIN__': getg0.f:(.text+0x73c): undefined reference to `readin_' collect2: ld returned 1 exit status...
  18. A

    Fortran Troubleshooting Function Call Error in Subroutine

    So I am making a module to run in the main program. The module contains a subroutine and several functions. I want to call one of the functions into the subroutine but it is not getting recognized. I am just doing Real :: fct %fct is the function name that returns a real value but when...
  19. A

    Fortran How can I fix incorrect output from my Fortran subroutine for a test matrix?

    Hi all, So I have a program that calls a module that contains a subroutine so I can make a matrix. When I write the matrix in the subroutine (commented out part in module) I get the right output, but when the main program tries to output it is some insanely small or large numbers. I am new to...
  20. H

    Fortran Why fortran program produces different values, when used as a subroutine ?

    Hello fortran users I, have a problem regarding values that my program produces. Let me detailed it to you. See, i have createrd a separate fortran program to calculete area under a given function i.e. it perfroms integration. When this program runs independently it gives me area of the...
  21. V

    Fortran [Fortran] Passing elements of array to subroutine

    I have one main program where I have calculated some array, say A(i,j) If I want ot use the values in subroutine, and calculate some more values and give back the result to main program, how my syntax should be? I tried this way. Its not working. program a...
  22. S

    How to pass derived type argument to a subroutine

    Hi all ! I have a derived type as: TYPE type_atom_type_info integer:: type_code character(len=4)::type_cname real(kind=dbl)::mass end TYPE type_atom_type_info I have an array of this type as: TYPE(type_atom_type_info) atom_type_info(250) I want to pass this array to a...
  23. S

    Subroutine in subroutine argument problem

    I'm having a problem with the argument of a subroutine which I didn't write myself, but should be working as intended. I'm supposed to write my own subroutine named funcs(a,b,c). Then this subroutine that I got has funcs as one of it's arguments subr(a,b,c,funcs,..) and it says in the...
  24. L

    Fortran Fortran - Allocate array in subroutine

    I try to write a small program to allocate a variable in small subroutine and I've passed it, now i allocate array in a loop or many loops but i don't have any ideas about it. Has anyone done like me , can share some experiences ??
  25. A

    Fortran Fortran90: Subroutine DSYEV and associating eigenvalues and eigenvectors.

    Greetings. I am using the LAPACK (Linear Algebra Package) software package to find the eigenvalues and eigenvectors of a large symmetrical real matrix. Specifically, I calculate a scalar from each eigenvector, and I want to graph it against its associated eigenvalue. I am using the subroutine...
  26. F

    Looking for fortran subroutine for I Bessel function with negative order

    Hello all, I am developing a new analytical solution for a problem in flow in porous media, and I need to write it in Fortran. This solution contains the modified Bessel function of the first kind, I_n(x). The order n is a real number, and it can be both negative and positive. The argument x...
  27. N

    Fortran Fortran 77 subroutine for calculating spherical harmonics

    Hey guys I am trying to understand a code for a Fortran 77 subroutine which calculates spherical harmonics using the CERN library RASLGF for legendre functions. The code looks like this subroutine harmonics(max,theta,phi,Yr,Yi) implicit none integer max,k,nn,n,grens...
  28. M

    Fortran Join 2 FORTRAN Codes: Problem Solved w/ Intel Compiler & Linux

    I have a problem,actually I want to join two FORTRAN codes.One of them is main and the other calculates some issues(geophysics stuff).I am not sure that the second code can be represented as subroutine because it has 2000+ lines,with 10 subroutines inside itself.How to solve this?Is it possible...
  29. A

    Fortran Fortran, subroutine with allocatable, intent(out) array

    Hi, I have a rather large program written in fortran 77/90. In one of the subroutines a lot of local dynamic arrays are declared. In order to make this program able to calculate more stuff, I need to bring these dynamic arrays back into the main program. I have tried to define the...
  30. J

    Fortran Fortran 77 - subroutine in separate file

    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...
  31. F

    How to pass array element form a subroutine to main function

    Hi guys. I want to transfer each elements of an array (which is declared in one subroutine)to main function using common block technique. When i am doing so i go always a constant value in the array in the main function. Suppose i have one array bparz(i) which has different values in one...
  32. N

    F90 - diagnolize square matrix subroutine

    Hi all, Does anyone have/know the location of a subroutine for returning the diagonalized matrix of square, real array? The array is a covariance matrix so should be diagonalizable in all cases. Thanks, Natski
  33. M

    How to run dgemms subroutine on mac computer

    Homework Statement I wrote a program to run the dgemul subroutine on my mac to find the matrix product for matrix a and matrix b, outputted as matrix c, just like the reference manual told me to. Being a mac user, I added the accelerate framework to the file which contain all the lapack...
  34. M

    Trouble with BLAS subroutine dgemv ->vector- matrix product

    Trouble with BLAS subroutine dgemv -->vector- matrix product Hello all, So, I am trying to write some code that will compute the vector-matrix product using the BLAS subroutine dgemv. I have written the code below, but I am getting really wacky numbers! You will see that it takes array...
  35. L

    I would share my genetic algorithm subroutine

    Dear all, i wrote a fortran subroutine that implement a genetic algorithm in double precision. I would like share this code in order to improve and due to the lack of this kind (double precision) of genetic algorithm in fortran. My question is where i can do this? best regards
  36. N

    Fortran Quantile subroutine, Fortran 90

    Hi all, I have been Googling for a Fortran 90 quantile routine but to no avail... I need a subroutine to which one passes an 1-D array plus the quantile range required... e.g. "call quantile(array,0.5D0,value)" would give me the median of array. Does anyone know of a handy (and...
Back
Top