Presentation is loading. Please wait.

Presentation is loading. Please wait.

LAB-06 IF + Functions I Putu Danu Raharja

Similar presentations


Presentation on theme: "LAB-06 IF + Functions I Putu Danu Raharja"— Presentation transcript:

1 LAB-06 IF + Functions I Putu Danu Raharja raharja @kfupm.edu.sa
Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals

2 Rationale If you work on a program to solve a bigger problem, your program will become larger (more lines of code). A large program is harder to debug (so far we have written programs < 100 lines but we still make mistakes) Some of the code may be used several times. ICS101-LAB06-Term063

3 Subprogram A big problem can be solved by dividing it into smaller sub-problems and conquering the sub-problems A large program can be divided into simpler sub-programs that can be implemented and tested independently Sub-programs can be reused ICS101-LAB06-Term063

4 Terminologies Main program Sub-program Call & return Function
Subroutine ICS101-LAB06-Term063

5 Function A function consists of:
Function header Function body type FUNCTION fname (list of arguments) Declaration statements Executable statements Must have at least one RETURN statement Must be ended with an END statement ICS101-LAB06-Term063

6 Example: Area of a triangle
REAL FUNCTION TRAREA (A, B) REAL A, B TRAREA = A * B / 2 RETURN END REAL X, Y, AREA READ*, X, Y AREA = TRAREA(X, Y) PRINT*, 'Area = ', AREA ICS101-LAB06-Term063

7 Intrinsic Functions Functions which are available from the FORTRAN language (p. 63) SQRT(X) ABS(X) SIN(X) COS(X) TAN(X) EXP(X) LOG(X) LOG10(X) INT(X) REAL(K) MOD(M, N) ICS101-LAB06-Term063

8 Statement Function Expressed as:
fname (list of arguments) = expression Example: TRA (BASE, HEIGHT) = BASE * HEIGHT * 0.5 ICS101-LAB06-Term063

9 Exercises A year with 366 days is called a leap year. A year is a leap year if it is divisible by 4 (for example, 1980), except that it is not a leap year if it is divisible by 100 (for example, 1900); however, it is a leap year if it is divisible by 400 (for example, 2000). There were no exceptions before the introduction of the Gregorian calendar on October 15, 1582 (for example, 1500 was a leap year). Write a logical function to determine whether the given year is a leap year. ICS101-LAB06-Term063

10 Exercise Write a function that takes month and year as arguments and returns the number of days of that month. Write a function that takes two real numbers and tests whether they are the same up to two decimal places. Write a function that takes two times (hour and minute) in military format (09:00, 17:30) and computes the number of hours between the two times. You have to consider the case that the first time is later than the second time. ICS101-LAB06-Term063


Download ppt "LAB-06 IF + Functions I Putu Danu Raharja"

Similar presentations


Ads by Google