Presentation is loading. Please wait.

Presentation is loading. Please wait.

LAB-05 Function I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.

Similar presentations


Presentation on theme: "LAB-05 Function I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals."— Presentation transcript:

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

2 ICS101-LAB05-Term073 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.

3 ICS101-LAB05-Term073 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

4 ICS101-LAB05-Term073 4 Terminologies Main program Sub-program Call & return Function Subroutine

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

6 ICS101-LAB05-Term073 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 END

7 ICS101-LAB05-Term073 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)

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

9 ICS101-LAB05-Term073 9 Exercises Write a function to calculate the area of a circle; a function to calculate the circumference of a circle and the main program to test both of them. Write a function to calculate the area of a rectangle. Write a function called ISODD that checks if an integer X is odd or even. If X is odd, it returns.TRUE. Otherwise, it returns.FALSE.

10 ICS101-LAB05-Term073 10 QUIZ ………….. now


Download ppt "LAB-05 Function I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals."

Similar presentations


Ads by Google