You are on page 1of 8

Computing Fundamentals – I

(0th Semester) Lab Assignment # 05


Welcome to the Fifth laboratory session of computing
fundamentals-I course. Today’s task is to develop Writing
subprograms that can be called in main programs.
Subprograms
1 Study Materials
Go to canvas and see Lecture 09 on subprograms (functions
and subroutines) and attempt examples and exercises given
in other lectures containing words ‘Scientific computing’ in
their titles.

Before attempting the assignment on the last page, take some time to read the instructions and
supplementary material. You may have to attempt a problem based on a “new concept” that
have not been discussed in the class.

Important Tip:
Use many comments in your codes to describe your aims and actions in your program like
variables, calculations etc., to make your programs more readable.

Page 1 of 8
2 Subprograms
To avoid unnecessary repetition of code, FORTRAN makes use of subprograms. FORTRAN
has two kinds of subprograms – functions and subroutines. There are some of ready-made
functions intrinsic to FOTRAN. For instance, sin(x), min(x, y), abs(x) are some
of the intrinsic (built-in) functions. There are several advantages of breaking a program into
smaller subprograms:
 Code of subprogram can be reused.
 They are portable. They can be used in other programs and by other programmers.
 Subprograms make it easier to debug the programs.

2.1 Functions
It is the simplest type of a subprogram whose purpose is to calculate a single answer and return
the result. The main program calls the function and after the code of function is executed, the
control is returned back to the main program along with a value that is returned from the
function. Y=sin(x) is the calling statement, which tells the computer to use the function. The
computer transfers the value of x to the function and stores the answers in the variable y. There
are some distinct characteristics of functions:
1. They compute a value and associate it with the name of the function.
2. This value is subsequently returned to the calling program.
3. A function CANNOT return more than one value.
4. A function is executed by invoking it from another program unit. When the function
has finished executing, control is returned to the invoking program unit, which
continues from the point where the invocation occurred.
A function subprogram differs from main program in several ways:
1. It lies outside the main program and outside other subprograms.
2. Its variables are unknown outside the function body.
3. It starts with a special FUNCTION statement, which acts as a header, names the
function, declares the type of value returned and also specifies its arguments (these are
the inputs sent to the subprogram from the main program).
4. It has a body of code that assigns a value to the function’s identifier.
5. It has its own END statement.
A function subprogram is invoked in the same manner as a library function is invoked.

Page 2 of 8
Syntax of a function subprogram

Type FUNCTION function-name ( arguments )


.
.
.
Function-name = some value
.
.
.
RETURN
END

The Type specifier in the FUNCTION header tells FORTRAN what type of value is retuned by
the function. If function has no arguments, pair of empty parenthesis must follow the function
name. Somewhere inside the function body, a value of an appropriate type must be assigned to
the function identifier. The RETURN statement returns the control to the calling program.

Above example shows the function to calculate average of three real numbers, average of
squared numbers and square of average.

Page 3 of 8
This is the same program as before but with arguments of different types. The 3 rd line of
function explicitly declares the variable which is integer. It is a good practice to declare all
arguments in the function body and leave no liability to the default rule regarding variable
types.

2.2 Subroutines
A SUBROUTINE is a subprogram similar in most respects to a function. Like a function, a
subroutine has a list of dummy arguments used to exchange information between the subroutine
and a program unit referring to it. Unlike a function, a subroutine does not return a value via
its name (and therefore has no type), but it may return one or more values via its arguments. A
subroutine subprogram begins with a SUBROUTINE statement and ends with END. The
SUBROUTINE statement has the form:
SUBROUTINE name (argument_list)
where name and argument_list have the same meanings as in the FUNCTION statement. As for
a function, a subroutine must include at least one RETURN statement to return control to the
program unit referring to it. A subroutine is referenced by a CALL statement, which has the
form:

Page 4 of 8
CALL name (argument_list)
A function subroutine always returns only one value associated with the name of the function.
For more general-purpose subprograms, SUBROUTINE is used. Subroutines differ from
functions in following ways:
1. No value is associated with the name of the subroutine. Instead, a subroutine returns its
output values, if any, by modifying the values of some or all of its arguments.
2. Since a subroutine can modify more than one value so there is no type associated with
the subroutine name.
3. A subroutine is not invoked simply by writing its name in the calling statement. This is
because subroutine can return more than one value. We write a CALL statement to bring
it in action.
4. When a subroutine has no arguments, the parenthesis are omitted (different from
functions).

Syntax of a subroutine

SUBROUTINE subroutine-name ( arguments )


.
.
.
RETURN
END

The outputs of a subroutine are returned to the caller by means of the argument list. The
following example shows the use of three subroutines. It can clearly be seen that the variables
in the argument list are the ones that are modified. In the subroutine INPUT, the arguments X,
Y and Z are assigned the values and then the same values are used in subsequent calls to the
other subroutines.

Page 5 of 8
Page 6 of 8
3 Assignment Tasks
Since you have directly jumped here from the first page, let me ask you to go back a few steps
in Task 1.
Task 1. Write a half page summary of previous sections in urdu and show it to your lab
instructor.
Task 2. Compile and show the output of all the programs written in above description, and in
lectures on Subprograms and Computing applications.
Task 3. Write a FORTRAN program (and then a subprogram) to take a number as input from
user and calculate the value of sine of that number using ‘n’ terms of following series:
x3 x5 x7
sin( x)  x     ...
3! 5! 7!
This is to be done manually, suggest the least value of ‘n’ (number of terms) so that a
value computed from your function is accurate up to 5 decimal places as the value given
by Fortran’s intrinsic sin(x) function.

Task 4. Assuming D=a1b2-a2b1≠0, the solution of linear equations;

a1 x  b1 y  c1
a2 x  b2 y  c2

b2 c1  b1c2 ac a c
x y 1 2 2 1
is given by a1b2  a2b1 , and a1b2  a2b1

Write Fortran Subprogram which reads values a1, b1, c1 and a2, b2, c2 , then call another
subprogram that calculates the solution pair x, y.
Task 5. Write a program to print positive divisors of a number (taken as input from user).
Task 6. Develop a subprogram for Task 12, and use it to print positive divisors of each number
between A and B, where A and B are provided by users (A < B). This is a two step
program:
Desired Output

Page 7 of 8
Enter value of A: 10
Enter value of B: 13
Positive Divisors of 11
1, 11
Positive Divisors of 12
1, 2, 3, 4, 6, 12
Positive Divisors of 13
1, 13

Task 7. Perfect number is a positive integer which is equal to the sum of its divisors. e.g.
6=1+2+3. Therefore, 6 is a perfect number. Write a FORTRAN subprogram and pass
the number as the argument and check whether it’s perfect or not.
Task 8. Generate the terms of following sequences using DO Loop. Take the number of
terms to be computed as input from user:
i. 10, 8, 16, 14, 28, 26, 52, …
ii. 1, 4, 9, 16, 25, …
iii. 1, 2, 4, 5, 8, 9, 13, …
iv. 3, 3, 4, 8, 10, 30, 33, …
v. 1, 2, 3, 6, 11, 20, 37
Task 9. Write a FORTRAN subprogram to calculate the GCD of 3 numbers. First write a
program to compute GCD of two numbers. Make a subprogram for it. Use it for three
numbers.
Task 10. Write a FORTRAN function that returns the L.C.M of 4 input arguments. First write a
program to compute LCM of two numbers. Make a subprogram for it. Use it for three
numbers.

Page 8 of 8

You might also like