
Function names follow the same rules as for variable names: less than six letters or numbers and beginning with a letter. You need to declare the function name in the variable declaration section. You have to do one thing in the main program to use an external function. When this occurs you may write your own function subprogram. The intrinsic functions in FORTRAN are useful but there will be a time when there is no intrinsic function to meet your needs. One last item to remember, you must use result of a function call in an assignment statement or a PRINT statement, as shown in the examples above. It can be an expression or even a constant if you want to reference it again. Also, as shown by the SQRT function example above, the argument for a function does not have to be a variable. The compiler evaluates the expression, a**2+b**2, sends that value to the SQRT function, and places the answer in the variableĪs shown by the MAX function example above, a function may have one or more arguments but will only give one result. If a=2, b=4, c=1 and d=7, then M would have a value of 7. The compiler puts the maximum value of a, b, c and d into the variable M. The compiler calculates the value of sin x, adds 45 then puts the result into the variable Y, where x is in radians. The compiler evaluates the absolute value of T and prints it out The function name is followed by a list of inputs, also called arguments, enclosed in parenthesis: answer = functionname (argumentl, argument2. The general way to activate a function is to use the function name in an expression. There are over 40 intrinsic functions in FORTRAN and they are mainly concerned with mathematical functions. There are some functions which are written into FORTRAN and can be used without any special effort by you, the programmer.



The purpose of a function is to take in a number of values or arguments, do some calculations with those arguments and then return a single result. This is similar to composing an outline for an essay before writing the essay and will help keep you on track. To implement functions and subroutines, first write a main program that references all of the subprograms in the desired order and then start writing the subprograms. Once each piece is working correctly you then put the pieces together to create the whole solution.

Using subprograms allows you to tackle bite size pieces of a problem individually. Most problems that require a computer program to solve them are too complex to sit down and work all the way through them in one go. Functions and Subroutines Functions and Subroutinesįunctions and subroutines are FORTRAN's subprograms.
