ReAl :: x OK CHARACTER :: name OK, a 1 character name! CHARACTER(LEN=10) :: name OK, string length 10 REAL :: var-1 cannot have -1 in a declaration var_1.

Slides:



Advertisements
Similar presentations
Introduction to arrays
Advertisements

Modular Programming With Functions
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Procedural programming in Java
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 10 Introduction to Arrays
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Need for Arrays Exercise Read the IDs and the grades for all ICS 101 students. Compute and print the average of the students. Print the grades and IDs.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 2 Basic Elements of Fortan
Unit 171 Algorithms and Problem Solving  Introduction  Algorithm Design  Algorithm Properties  Algorithm Control Flow  Examples  Comparing Algorithms.
6 April, 2000 CS1001 Lecture 15 EXAM1 - results SUBPROGRAM - - revisit FUNCTION.
27 March, 2000 CS1001 Lecture 16 FUNCTIONS SUBROUTINES SCOPE MODULES EXTERNAL SUBPROGRAMS.
Chapter 10 Modules and programming with subroutines.
Revision – A simple program How to start a program? How to end a program? How to declare variables? What are the mathematical operators? How to start a.
Chapter 9 Modules and Programming with Functions.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.
21 April, 2000 CS1001 Lecture 27 Final Examination Review.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
LAB-10 1-D Array I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Functions. Program complexity the more complicated our programs get, the more difficult they are to develop and debug. It is easier to write short algorithms.
Large problems can be divided into smaller sub - problems ♦ each sub - problem can be solved separately in order to reach to the solution of the original.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Introduction to FORTRAN-90 University of Liverpool course.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Fortran: Specification Statements Session Six ICoCSIS.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Introduction to FORTRAN
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
E0001 Computers in Engineering Procedures: subprograms and functions.
Week 8. Today’s program n New stuff: –Recursion in F –Procedures as arguments –User-defined data types n Quiz #3.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Week 8 Improving on building blocks Recursive procedures.
Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:
CHAPTER 9 SUBPROGRAMS Subprograms are useful in the following cases: long complicated program  Subprograms make it readable and simple by dividing it.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Introduction to VB.NET 2005 Dr. McDaniel IDS4704 Spring 2005.
Week 3 Let's review! Fundamental data types List-directed input/output.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Procedures and Modular Programming Part # 2. Interface Block ► Functions do not have to be internal to main program ► They can be stand-alone ► In this.
Week 4: Organizational matters Register yourself to CIMS (Course Information and Management System) by clicking on Homework at hidiv.cc.itu.edu.tr/~F90.
Beginning Fortran Fortran (77) Advanced 29 October 2009 *Black text on white background provided for easy printing.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
FUNCTIONS IN FORTRAN For a complex program difficulties like writing it and debugging are encountered. These can be minimized by breaking main program.
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
Operator precedence.  Evaluate a + b * c –multiplication first? a + (b * c) –addition first? ( a + b) * c  Java solves this problem by assigning priorities.
Fortran MATTHEW CARSON. History of FORTRAN FORTRAN ; First High Level Language FORTRAN I 1957; First Compiled Language FORTRAN II ; Independent.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
19 - 2/25/2000AME 150L1 Solving Systems of Linear Equations.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions and Subroutines
Chapter 5 - Functions Outline 5.1 Introduction
Subroutine Comp 208 Yi Lin.
Midterm Review Programming in Fortran
mdul-cntns-sub-cmmnts2.f90
DATA TYPES AND OPERATIONS
REPETITION Why Repetition?
Presentation transcript:

ReAl :: x OK CHARACTER :: name OK, a 1 character name! CHARACTER(LEN=10) :: name OK, string length 10 REAL :: var-1 cannot have -1 in a declaration var_1 would be INTEGER :: 1a Cannot start a variable with a number. BOOLEAN :: loji No BOOLEAN type, use LOGICAL. CHARACTER(LEN=5) :: town = "Glasgow" OK(ish), town is set to Glasg. CHARACTER(LEN=*), PARAMETER :: city = "Glasgow“

LOGICAL :: wibble =.TRUE. OK CHARACTER(LEN=*), PARAMETER :: "Bognor“ Error, no variable name REAL, PARAMETER :: pye = 22.0/7.0 OK REAL :: a = 1., b = 2 OK, the value 2 is promoted to be 2.0 LOGICAL(LEN=12) :: frisnet Error cannot use LEN=12 with LOGICAL. CHARACTER(LEN=6) :: you_know = 'y'know" Unbalanced delimiters ' and ", the second ' will cause an error too. CHARACTER(LEN=6) :: you_know = "y'know"

HTMLF90CourseQuestionsnode22.html

CHAPTER 9 SUBPROGRAMS Subprograms are useful in the following cases: long complicated program  Subprograms make it readable and simple by dividing it into smaller programs. set of operations is to be repeated  Subprograms are used to avoid repetition.

FORTRAN language has two types of subprograms: FUNCTIONS: return single value. SUBROUTINES: return more than one value. 1) INTRINSIC functions example: ConstantSIN (10.0) VariableSIN (theta) ExpressionSIN (2.0*alfa + theta/3.0) Other functionSIN (ABS (a-b)) Functions: There are two types of functions:

2) USER-DEFINED functions Example: PROGRAM STATMENT_FUNCTION IMPLICIT NONE INTEGER,DIMENSION(3,4)::RENT INTEGER :: SET,I,J,L,K SET(K,L) = K +L/3 DO I=1,3 DO J=1,4 RENT(I,J)=SET(I,J) END DO PRINT*,'ELEMENTS OF ARRAY RENT ARE:' DO I=1,3 PRINT*,(RENT(I,J),J=1,4) END DO STOP END PROGRAM STATMENT_FUNCTION

External Function Subprogram External function subprogram can be made accessible to a program unit by attaching it after the END statement of the main program. EXAMPLE Write a main program to read three values and prints their average. The average values should be calculated using an external function subprogram. PROGRAM Test REAL :: Test 1, Test 2, Test 3, Ave READ *, Test 1, Test 2, Test 3 PRINT 5, Ave (Test 1, Test 2, Test 3) 5FORMAT (1X, ‘Average=’, F5.2) STOP END PROGRAM Test REAL FUNCTION Ave (X, Y, Z) REAL :: X, Y, Z Ave=(X+Y+Z)/3.0 RETURN END FUNCTION Ave

EXAMPLE : Follow the execution of the following program and show its output? PROGRAM FUNCTION_SUBPROGR AMS IMPLICIT NONE REAL :: A,B,C,TIME,ADD A = 1.0 B = 2.0 C = 3.0 TIME = 2.0*ADD(A,B,C) PRINT 10, TIME 10 FORMAT (1X, 'TIME=', F6.2) STOP END PROGRAM FUNCTION_SUBPROG RAMS ! ! FUNCTION SUBPROGRAM REAL FUNCTION ADD(X,Y,Z) REAL :: X,Y,Z Y = X +Y Z = Y + Z X = Z + X ADD = X + Y + Z RETURN END FUNCTION AD

Subroutine Subprogram  Subroutines are modulus written to perform operations that cannot be performed by a function. For example, if several values need to be returned from a module, a sub-routine is used.  A subroutine is also used for operations that do not compute values, such as reading the values in a data file.  Subroutines may be either internal or external. External Subroutines Subprogram External subroutines can be made accessible to a program unit by attaching it after the END statement of the main program.

Subroutines Rules 1.A subroutine does not represent a value; thus, its name should be chosen for documentation purposes and not to specify a real or integer value. 2.A subroutine is referenced with an executable statement whose general form is CALL subroutine name (argument list) 3.The first line in a subroutine identifies it and includes the subroutine name and argument list, as follows: SUBROUTINE name (argument list) 4.A subroutine uses the argument list for subroutine inputs and values returned to the calling program. arguments in CALL statement  actual arguments. arguments in SUBROUTINE  dummy arguments. The arguments in the CALL statement must match in type, number, and order those used in the subroutine definition.

5.A subroutine may return one value, many values, or no value. Similarly, a subroutine may have one input value, many input values, or no input value. 6.Arguments are the link between the main program and the subroutine. Thus, the choice of subroutine statement numbers and variable names is independent of those in the main program. Variables used in subroutine and not subroutine arguments  local variables  their values are inaccessible from main program. 7. The subroutine, like the function, requires a RETURN statement to return control to main program.It also requires an END statement. 8.A subroutine may reference other functions or call other subroutines, but it cannot call itself.

EXAMPLE: Write a Fortran program which uses a subroutine program to calculate the average, the minimum value and the maximum value of an array X with number of elements N represents the scores of students grade. PROGRAM Scores INTEGER :: N, I REAL :: Tests (100), Ave,Min,Max READ*, N READ*, (Tests(I), I=I,N) CALL Stat (Tests, N, Ave, Min, Max) Print 5, Ave, Min, Max 5Format (1X, 3F10.3) END PROGRAM Scores SUBROUTINE Stat (X, M, Xave, Xmin, Xmax) INTEGER :: M, I REAL :: X(M), Xave, Xmin, Xmax, Sum Sum=X(1) Xmax=X(1) Xmin=X(1) DO I=2,M Sum=Sum+X(I) IF (X(I) < Xmin) Xmin=X(I) IF (X(I) > Xmax) Xmax=X(I) END DO Xave=Sum/M RETURN END SUBROUTINE Stat

EXAMPLE: Rent A Car! Program Rented_Car_cost Integer::Dis Real::Total,Average Character(20)::Name, Model Read 2, Name, Model,Dis 2 Format(2(A20,/),I6) Call Rent(Dis,Total,Average) Print 5,"Customer Name:",Name,'Car Model:',Model 5 Format(2x,2(2A20,/)) Print 10,'Travel Distance (km):',Dis,'Total Rent (SR):',Total 10 Format(2x,A22,I6,/,A20,F7.2) Stop End Program subroutine Rent(D,TR,Avg) Integer::D Real::TR,Avg If (D.LE.100) THEN TR= D*0.5 Else if (D.LE.300) THEN TR= 100*0.5+(D-100)*0.3 Else TR= 100* *0.3+(D- 300)*0.2 End if Avg=TR/ D end subroutine Rent

EXAMPLE Follow the execution of the following program and show its output? PROGRAM SUBROUTINE_EXAMPLE IMPLICIT NONE INTEGER,DIMENSION (10):: K INTEGER::I DO I=1,10 K(I)=I END DO CALL MODIFY (K,10) PRINT*,"NEW VALUES OF K ARE:" PRINT 15, (K(I),I=1,10) 15 FORMAT(1X,5I5) END PROGRAM SUBROUTINE_EXAMPLE SUBROUTINE MODIFY(L,N) INTEGER :: I,N INTEGER, DIMENSION (N):: L DO I=1,N L(I)=MOD(L(I),4) END DO RETURN END SUBROUTINE MODIFY

PROGRAM MAIN_ABC Read*, A,B ANSWER= C(A,B) WRITE(6,100)‘C= ',ANSWER 100 FORMAT(1X,A3,F10.2) END !END OF PROGRAM MAIN_ABC !BEGINNING OF FUNCTION C FUNCTION C(x,y) C= SQRT(x**2 + y**2) RETURN END !END OF FUNCTION C EXAMPLE: Write a program that calculate the following: Use a function subprogram

implicit none integer :: N, square, cube print *, "Please type an integer and press RETURN" read *, N call square_and_cube( N, square, cube ) print * print *, N, " squared is ", square print * print *, N, " cubed is ", cube end subroutine square_and_cube( i, i_squared, i_cubed ) implicit none integer, intent(in) :: i integer, intent(out) :: i_squared, i_cubed i_squared = i**2 i_cubed = i**3 end subroutine EXAMPLE: Write a subroutine that calculate the sequare and cube of any number.

Exercise: Write a complete FORTRAN 90 program to convert temperatures on the Celsius scale to the corresponding temperatures on the Fahrenheit scale according to the relationship: Fahrenheit = 1.8*Celsius + 32