3: Strings, Functions and Subroutines

3.1 Character strings

A character string consists of any number of characters enclosed in single apostrophes. Thus

	'THIS IS A CHARACTER STRING'

is a string of length 26 (the space is a character). It is also a character constant which has the value THIS IS A CHARACTER STRING.

Character strings can be stored in variables which must be declared as CHARACTER variables at the beginning of the program. The maximum length of the string must also be specified, unless its length is only one.

	CHARACTER*26 PHRASE,SENTENS(10)
	PHRASE='THIS IS A CHARACTER STRING'

Here PHRASE is a character variable of length 26 and SENTENS is a character array of 10 elements each of length 26.

	SENTENS(1)=PHRASE

will move the character string into element 1 of SENTENS.

It is possible to handle parts of a string, ie substrings, by specifying the first and last position of the characters of interest within the string:

      CHARACTER LETTER(80)
      CHARACTER*26 PHRASE
      PHRASE='THIS IS A CHARACTER STRING'
      LETTER(1)=PHRASE(1:1)
      LETTER(2)=PHRASE(2:2)
      DO 10 I=3,26
        LETTER(I)=PHRASE(I:I)
  10  CONTINUE
      .
      .
      .

LETTER is a character array of 80 elements each of length one character. LETTER(1) contains the first character of the string, namely, T. LETTER(2) the second character, etc.

The only operator allowed in character expressions is the concatenation operator // which has the effect of combining two strings together to form a longer string:

	CHARACTER*26 PHRASE(4)
	PHRASE(1)='THIS IS A CHARA'
	PHRASE(2)='CTER STRING'
	PHRASE(3)=PHRASE(1)//PHRASE(2)
	PHRASE(4)=PHRASE(3)(21:26)

PHRASE(3) now contains the complete string, and PHRASE(4) contains the substring 'STRING'. Note in this example the declared length of the character variables is longer than some of the actual strings. In this case the string is placed in the lefthand most part of the variable. Thus PHRASE(2) contains the string 'CTER STRING '.

Character strings and variables have a value, and hence can be tested in an IF statement for example:

	CHARACTER*3 ANS
	ANS='YES'
	IF(ANS.EQ.'YES')THEN
	.
	.
	.
	ENDIF

There are a few functions for dealing with character strings:

	LEN(a)     the length of the character string a. Result is an integer.
	ICHAR(a)   the numerical value of the first character in string a. 
		   Integer result according to how characters are coded (usually ASCII code).
	CHAR(I)    the character whose numerical value is I. Thus if a=CHAR(I) then I=ICHAR(a).
	INDEX(a,b) the starting position within string a of the first appearance of string b. 
		   The result is integer (0 if b does not occur in a). 

3.2 Functions and Subroutines

Defining your own functions can be very useful if an arithmetic expression occurs very frequently in the program. If a whole set of statements, performing a particular calculation, are repeated in different places in the program then a subroutine is an efficient way to incorporate these statements. Indeed, the use of subroutines and functions can improve the understanding of the program and the ease of writing it by breaking the program into modules. It is good programming practice to write your program as best you can in a modular form.

3.2.1 Statement function

This is a simple form of user-defined function which is just like a mathematical function you might use in a calculation many times. For example, calculating the volume of a sphere:

	VOLSPH(R)=1.33333*3.14159265*R**3

Notice the function name is REAL. This statement must appear after the declarations at the beginning of the program but before any executable statements. Later on in the program the function is called as you would expect:

	.
	.
	RNEW=R1+R2
	DELTAV=VOLSPH(R1)+VOLSPH(R2)-VOLSPH(RNEW)
	.

3.2.2 function subprogram

If there are many statement associated with the calculation of the value of a function, then it is desirable to create a function subprogram. This is a quite separate part or module of the program which is self-contained, and enclosed within it own FUNCTION and END statements.

Defining a function subprogram called name (integer function if name begins with I, J, K, L, M, or N):

	FUNCTION name (dummy arguments)
 	.
	.
	RETURN
	.
	.
	END

END acts as a return to the calling program, or one can explicitly use the RETURN statement at any time.

The dummy argument list provides a means for data to be input to the subprogram. Variables and arrays declared within the subprogram are local to it. Thus in the example below the variable N in the main program is completely separate from the variable N in the function. However, you must be much more cautious about I in the subprogram, since it is linked to the main program through the actual argument in the function call, in this example, N, IR and N-IR.

Modifying I in the subprogram will alter the value of the corresponding varible in the main program when the function returns. This is not a problem in the example below. As a rule don't alter the value of dummy arguments in function calls. Also FORTRAN does not allow a function to call itself (known as recursion).

C     MAIN PROGRAM TO CALCULATE BINOMIAL COEFFICIENTS
C
      PRINT*,'INPUT N & R'
      READ*,N,IR
      COEFF=FACT(N)/(FACT(IR)*FACT(N-IR))
      PRINT*,'THE ANSWER IS',COEFF
      END
C
C     FUNCTION TO CALCULATE I!
C
      FUNCTION FACT(I)
      F=1
      IF (I.GT.1)THEN
        DO 10 N=1,I
          F=F*N
 10     CONTINUE
      ENDIF
      FACT=F
      END

Though this is a good illustration of the use of the function, it is not an efficient way of generating the binomial coeff. Why? Nor is it likely to work for large values of N and/or R. Why? How could it be done better?

3.2.3 Subroutines

A development of the function subprogram, which allows more than one value to be returned as a result, is the subroutine. Similar in definition and operation to the function subprogram:

	SUBROUTINE name (dummy arguments)
	.
	.
	RETURN
	.
	END

The subroutine is called from the main program by:

	CALL name (actual arguments)

Unlike the function the results are returned to variables in the argument list, and not to the name of the function (hence the first letter of the subroutine name has no influence on the type of the results, integer or real).

The above program to calculate binomial coefficients, using a subroutine call would look like:

C     MAIN PROGRAM TO CALCULATE BINOMIAL COEFFICIENTS
C
      PRINT*,'INPUT N & R'
      READ*,N,IR
      CALL BINOM(COEFF,N,IR)
      PRINT*,'THE ANSWER IS',COEFF
      END
C
C     SUBROUTINE TO CALCULATE BINOMIAL COEFFICIENT
C
      SUBROUTINE BINOM(CO,I,J)
      CO=FACT(I)/(FACT(J)*FACT(I-J))
      END
C
C     FUNCTION TO CALCULATE I!
C
      FUNCTION FACT(I)
      F=1
      IF (I.GT.1)THEN
        DO 10 N=1,I
          F=F*N
 10     CONTINUE
      ENDIF
      FACT=F
      END

Intrinsic functions in FORTRAN:

	SIN(X)		TAN(X)		EXP(X)		LOG10(X)
	COS(X)		SQRT(X)		LOG(X)		ASIN(X)
	ACOS(X)		ATAN(X)