Here are a few examples of functions:

- The following function has a name
**Sum**and three formal arguments**a**,**b**and**c**. It returns an**INTEGER**function value. The**INTEGER, INTENT(IN)**part indicates that the function takes its input value from its three formal argument. Then, the function uses the value of these formal arguments to compute the sum and stores in**Sum**, the name of the function. Since the next statement is**END FUNCTION**, the function returns the value stored in**Sum**.INTEGER FUNCTION Sum(a, b, c) IMPLICIT NONE INTEGER, INTENT(IN) :: a, b, c Sum = a + b + c END FUNCTION Sum

**a**,**b**and**c**are 3, 5, and -2, respectively,**Sum**will receive 6 (=3+5+(-2)) and the function returns 6. - The following function has a name
**Positive**with a**REAL**formal argument. If the argument is positive, the function returns**.TRUE.**; otherwise, the function returns**.FALSE.**LOGICAL FUNCTION Positive(a) IMPLICIT NONE REAL, INTENT(IN) :: a IF (a > 0.0) THEN Positive = .TRUE. ELSE Positive = .FALSE. END IF END FUNCTION Positive

**LOGICAL**assignment. In the following, if**a > 0.0**is true,**.TRUE.**is stored to**Positive**; otherwise,**Positive**receives**.FALSE.**LOGICAL FUNCTION Positive(a) IMPLICIT NONE REAL, INTENT(IN) :: a Positive = a > 0.0 END FUNCTION Positive

- The following function,
**LargerRoot**, takes three**REAL**formal arguments and returns a**REAL**function value. It returns the larger root of a quadratic equation*ax*^{2}+*bx + c = 0*.REAL FUNCTION LargerRoot(a, b, c) IMPLICIT NONE REAL, INTENT(IN) :: a REAL, INTENT(IN) :: b REAL, INTENT(IN) :: c REAL :: d, r1, r2 d = SQRT(b*b - 4.0*a*c) r1 = (-b + d) / (2.0*a) r2 = (-b - d) / (2.0*a) IF (r1 >= r2) THEN LargerRoot = r1 ELSE LargerRoot = r2 END IF END FUNCTION LargerRoot

**d**,**r1**and**r2**if they are needed. - The following function,
**Factorial()**, has only one**INTEGER**formal argument**n**>= 0, and computes and returns the factorial of**n**,**n!**.INTEGER FUNCTION Factorial(n) IMPLICIT NONE INTEGER, INTENT(IN) :: n INTEGER :: i, Ans Ans = 1 DO i = 1, n Ans = Ans * i END DO Factorial = Ans END FUNCTION

**Factorial**is not used in any computation. Instead, a new**INTEGER**variable**is used for computing****n!**. The final value of**Ans**is stored to**Factorial**before leaving the function.If

**Factorial**is involved in computation like the following:INTEGER FUNCTION Factorial(n) IMPLICIT NONE INTEGER, INTENT(IN) :: n INTEGER :: i Factorial = 1 DO i = 1, n Factorial = Factorial * i END DO END FUNCTION

- The following function
**GetNumber()**does not have any formal arguments and returns an**INTEGER**function value. This function has a**DO**-loop which keeps asking the user to input a positive number. The input is read into the function name. If this value is positive, then**EXIT**and the function returns the value in**GetNumber**. Otherwise, the loop goes back and asks the user again for a new input value.REAL FUNCTION GetNumber() IMPLICIT NONE DO WRITE(*,*) 'A positive real number --> ' READ(*,*) GetNumber IF (GetNumber > 0.0) EXIT WRITE(*,*) 'ERROR. Please try again.' END DO WRITE(*,*) END FUNCTION GetNumber