Control is returned to the statement immediately following the function reference. The input to the func tion is the argument list. The output from the function is contained in the function's name. That is, once the func tion is executed, the result of the computing procedure is stored in the function's name. Note that the actual argu ment list matches the dummy argument list in number, or der, and data type. If function ABC is to store an integer value, the following form of the function statement is re quired:.
Likewise, if the function. A character function is also possible. For instance, if a function TIME is to store a character value of length equal to 40 characters, the following form of the function state ment is required:.
Note that. In this example, the actual and dummy arguments are variables and have the same name A and B. A variation using constants for the actual arguments is shown in the next example. Like the function subprogram, a subroutine has zero or more RETURN statements to return control to the calling program whenever it is required. Control is returned to the statement immediately following the respective CALL statement. The subroutine name is not a scalar variable and cannot store a value.
Both input and output to the subroutine are contained in the argument list. Unlike a function, which is referenced by simply in voking its name, a subroutine is referenced by using a CALL statement Table 1. Example 1 reads the three coefficients A, B, and C of a quadratic equation, calls subroutine QUADR to calculate and write the roots, whether real or complex. Notice that the actual and dummy arguments have the same name. A variation of the above example using different names for actual and dummy arguments is shown below.
WRITE 6, A variable name which is used exclusively within a subroutine for instance, DISC in both examples , cannot be referenced in the calling program, because it is not an argument, and therefore, it was not passed back to the call ing program. In other words, the calling program has not been informed of the value of DISC. However, the vari able FLAG can be referenced in the calling program, be cause it is an argument, and it was passed back to the call ing program.
A RE TURN statement is used when the subprogram logic re quires that control be returned to the calling program before it has had the chance to reach the END statement. Multiple RETURN statements are used when the subpro gram logic requires that control be returned to the calling program from one of many places within the subroutine. Recall how a subroutine connects an actual argument list with its dummy argument list, storing corresponding argu ments in the same storage location.
This is why corre sponding argument lists should match in number, order, and data type. Inside the subroutine, the value of an argu ment has the dummy variable name; outside the subrou tine, it has the actual variable name.
In essence, it is the same value, but with two names. A COMMON statement is a way of defining common areas of physical storage, to be accessed by two or more program units. A common block is a contiguous area of physical stor age that may be accessed by one or more program units.
The label identifies the named common block and makes it unique among common blocks. An executable program may have only one blank common block. The above statement can also be written in two sepa rate lines or statements:. Two or more corresponding common blocks, which have the same label but are placed in different program units, have the same physical storage area and can share information when the program units are part of one execu table program.
Two or more common blocks may have the same named or blank label. In this case, the variable list following each successive appearance of the named or blank label is treated as a continuation of the block associated with the named or blank label.
A common block label is a variable. It may have the same name as a variable, but not the name of a function, subroutine, or entry See Section 9. Variables can be shared by program units by passing:. Some as arguments and the rest in common blocks, as in the first example of this section. All of them in common blocks, as in the second example of this section.
END IF. The results of the subroutine computa tions the roots of the quadratic are passed as arguments. There is no need to pass any arguments. The use of different variable names in corresponding common blocks is solely to provide flexibility in naming variables, to allow main program and subprogram to be developed independently of one another, say, by different people.
Real arrays A and B in the main program share the same storage space with real arrays X and Y in the subpro gram. In the main pro gram, X would be referred with the name A; conversely, in the subprogram, A would be referred with the name X; and so on.
Although the variables have different names, they match in number of elements, order, and data type. This example further shows the correspondence in number of elements, order, and data type. Is your data what you think it is? Difference between subroutine and functions by madtoperl Friar. Need Help?? Comment on Difference between subroutine and functions Replies are listed 'Best First'.
Wikipedia is a good friend. The name method is commonly used in connection with object-oriented programming, specifically for subroutines that are part of objects. No value is associated with the name of a subroutine,while the name of a function subprogram must have a value, numerical or logical.
A subroutine may be invoked only by a special calling statement-the call statement. The name of a function subprogram may be used in the same way as library functions,,i. Since a function subprogram computes atleast one value, it must contain a return statement. The way I always explain it: CODE objects are declared as subroutines, but used either as functions or methods.
Nor can you write a function or method, except as a subroutine. Please apply a little thought about what you are doing. The two words mean the same thing. They're synonyms. Nitpick--ITYM "synonym".
A function is not necessarily a computing term -- in the English language it could mean anything from something your body does to a social event. As diotalevi said, they are homonyms in terms of computing. My day has gone no better than usual, but at least it wasn't subroutine. The following example illustrates the use and definition of a user-defined function to compute the distance between a point x,y and the origin 0,0.
Fortran subroutines are similar to functions, except that they can return more than one variable or array to the calling program. Another difference is that the output values are returned via the arguments to the subroutine. Subprograms, functions and subroutines In the design of larger programs, the typical approach is to divide the program into several smaller, more manageable tasks.
Call function "dist".
0コメント