ICS 101 - Computer programming
Lab # 6
( Functions ) II
Objectives
Special Cases of Functions
Function | Function Value | Comment |
SQRT(X) | Square Root of X | X is a real argument |
ABS(X) | Absolute Value of X | |
INT(X) | Integer Value of X | Converts a real to an integer |
REAL(K) | Real Value of X | Converts an integer to a real |
MOD(M, N) | M - M / N * N | Modulo function |
Syntax
where
fname
is the name
of the function
a list of arguments is the optional list of dummy arguments
expression
computes the
function
Exercise 1
Write a program which prompts for and reads a positive integer N. It then calls a logical statement function MLTPL3 which returns the value .TRUE . if the integer N is divisible by 3. The program then prints one of the following messages DIVISIBLE BY 3 or NOT DIVISIBLE BY 3 accordingly.
Exercise 2
Write a function subprogram COST that computes the cost of postage according to the following: 2.50 Riyals for any weight equal to or less than a kilogram. Every additional kilogram is charged 3.0 Riyals , plus 100 Riyals extra charge If the customer wants fast delivery. The arguments to the function are the weight of the package and a logical variable FAST indicating fast delivery. Write a main program to test the function.
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