ICS 101 - Computer programming

Lab # 5

( Functions ) I

Objectives

•    FUNCTION

The problem tasks are divided into sub-tasks. In FORTRAN each sub-task can be implemented as a separate module called FUNCTION or SUBROUTINE.

Syntax where

Type: type of return value (INTEGER , REAL...) returns by the function

list of arguments: zero or more arguments passed to the function

Example Exercise 1

Write a logical function TRINGL that checks whether three given angles (in degrees) form a triangle or not. Write a main program that reads three angles from the input and uses the function TRINGL to output one of the following messages: ANGLES FORM A TRIANGLE or ANGLES DO NOT FORM A TRIANGLE accordingly .

Exercise 2

Write a main program, which reads the radius of a circle. Then it calls a real function called AREA to compute the area of a circle (pr2). The main program should print the result.

Note: PI = 3.14159

Exercise 3

A function f (x, y) is defined as:

ì   (x2+ y2 ) / (x - y)         if x  ≠  y

f(x,y) =  í

î    2x2 + y + 3                   if x = y

Write a main program to read X and Y and print the value of f(X,Y). The value should be computed by a function subprogram invoked by the main program.

Test your program with the following data:

4. 0          2 .0

6. 5          3. 5

2 . 0         7. 0