Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Introduction to Procedures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul -

Similar presentations


Presentation on theme: "Chapter 9 Introduction to Procedures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul -"— Presentation transcript:

1 Chapter 9 Introduction to Procedures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel: +90 (212 285 6519) Fax: +90 (212 285 6508) E-mail: takinaci@itu.edu.tr

2 Introduction to Procedures In Chapter 3, we learned the importance of good program design. The basic technique that we employed is top-down design. In top-down design, the programmer starts with a statement of the problem to be solved and the required inputs and outputs. Next, he or she describes the algorithm to be implemented by the program in broad outline, and applies decomposition to break the algorithm down into logical subdivisions called subtasks Fortunately, Fortran has a special mechanism designed to make subtasks easy to develop and debug independently before building the final program. It is possible to code each subtask as a separate program unit called an external procedure, and each external procedure can be compiled, tested, and debugged independently of all of the other subtasks (procedures) in the program. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 2

3 Introduction to Procedures There are two kinds of external procedures in Fortran: subroutines and function subprograms (or just functions). Subroutines are procedures that are invoked by naming them in a separate CALL statement, and that can return multiple results through calling arguments. Function subprograms are procedures that are invoked by naming them in an expression, and whose result is a single value that is used in the evaluation of the expression. Both types of procedures will be described in this chapter. Well-designed procedures enormously reduce the effort required on a large programming project. Their benefits include: 1.Independent testing of subtasks. 2.Reusable code. 3.Isolation from unintended side effects. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 3

4 Introduction to Procedures Subroutines A subroutine is a Fortran procedure that is invoked by naming it in a CALL statement and that receives its input values and returns its results through an argument list. The general form of a subroutine is © 2010, Dr. ALİ CAN TAKİNACI Slide No: 4 Name of the subroutine Dummy arguments End of the subroutine

5 Introduction to Procedures Any executable program unit may call a subroutine, including another subroutine. However, a subroutine may not call itself unless it is declared to be recursive; recursion will be explained in Chapter 13.) To call a subroutine, the calling program uses a CALL statement. The form of a CALL statement is where the order and type of the actual arguments in the argument list must match the order and type of the dummy arguments declared in the subroutine. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 5

6 Introduction to Procedures © 2010, Dr. ALİ CAN TAKİNACI Slide No: 6

7 Introduction to Procedures The INTENT Attribute Dummy subroutine arguments can have an INTENT attribute associated with them. The INTENT attribute is associated with the type declaration statement that declares each dummy argument. The attribute can take one of three forms: © 2010, Dr. ALİ CAN TAKİNACI Slide No: 7

8 Introduction to Procedures The purpose of the INTENT attribute is to tell the compiler how the programmer intends each dummy argument to be used. Some arguments may be intended only to provide input data to the subroutine, and some may be intended only to return results from the subroutine. Finally, some may be intended both to provide data and return results. The appropriate INTENT attribute should always be declared for each argument. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 8

9 Introduction to Procedures Variable Passing in Fortran: The Pass-by- Reference Scheme Fortran programs communicate with their subroutines using a pass-by- reference scheme. When a subroutine call occurs, the main program passes a pointer to the location in memory of each argument in the actual argument list. The subroutine looks at the memory locations pointed to by the calling program to get the values of the dummy arguments it needs. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 9

10 Introduction to Procedures Passing Arrays to Subroutines There are three possible approaches to specifying the length of a dummy array in a subroutine. 1. Explicit-shape dummy array 2. Assumed-shape dummy arrays 3. Assumed-size dummy array. Not reccomended © 2010, Dr. ALİ CAN TAKİNACI Slide No: 10

11 Introduction to Procedures Passing Character Variables to Subroutines When a character variable is used as a dummy subroutine argument, the length of the character variable is declared with an asterisk. Since no memory is actually allocated for dummy arguments, it is not necessary to know the length of the character argument when the subroutine is compiled. A typical dummy character argument is shown below: © 2010, Dr. ALİ CAN TAKİNACI Slide No: 11

12 Introduction to Procedures FORTRAN FUNCTIONS A Fortran function is a procedure whose result is a single number, logical value, character string, or array. The result of a function is a single value or single array that can be combined with variables and constants to form Fortran expressions. These expressions may appear on the right side of an assignment statement in the calling program. There are two different types of functions: intrinsic functions (sin(x), log(x)) and user defined functions (or function subprograms). © 2010, Dr. ALİ CAN TAKİNACI Slide No: 12

13 Introduction to Procedures © 2010, Dr. ALİ CAN TAKİNACI Slide No: 13


Download ppt "Chapter 9 Introduction to Procedures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul -"

Similar presentations


Ads by Google