1. User-Defined Functions & Subroutines Part 1 Outline

Size: px
Start display at page:

Download "1. User-Defined Functions & Subroutines Part 1 Outline"

Transcription

1 User-Defined Functions Subroutines Part 1 Outline 1. User-Defined Functions Subroutines Part 1 Outline 2. Intrinsic Functions Are Not Enough 3. User-Defined Functions 4. A User-Defined Function for Mean 5. Flowchart for Mean Function 6. User-Defined Function Definitions 7. User-Defined Function Definitions: Declarations Are Valid Only Within the Unit Where They Occur 8. User-Defined Function Definitions: FUNCTION and END FUNCTION Statements 9. User-Defined Function Definitions: Return Type 10. User-Defined Function Definitions: List of Arguments 11. User-Defined Function Definitions: INTENT Attribute 12. User-Defined Function Definitions: Argument Order for Arrays 13. User-Defined Function Definitions: Local Variables Named Constants 14. User-Defined Function Definitions: Assigning a Return Value to the Return Variable 15. An Important Point About Declarations Inside Functions 16. General Form of User-Defined Function Definitions 17. Using a User-Defined Function: Example 18. Using a User-Defined Function: Example Run 19. Jargon: Program Unit Function Unit 20. Another User-Defined Function Example 21. Using a User-Defined Function: Example #1 22. The EXTERNAL Declaration 23. Actual Arguments Formal Arguments 24. Using a User-Defined Function Example #2: Array Intrinsic Functions Are Not Enough Often, we have a particular kind of value that we need to calculate over and over again, under a variety of circumstances. For example, in our recent project, we had to calculate the mean, variance and standard deviation, which are common functions that come up in a lot of contexts: OU_opponent_score_sum = 0.0 DO game = first_game, number_of_games OU_opponent_score_sum = OU_opponent_score_sum + OU_opponent_score(game) END DO!! game = first_game, number_of_games OU_opponent_score_mean = OU_opponent_score_sum / number_of_games We know that the mean calculation is always the same. So why should we have to write the same piece of code over and over and over and over and over? Wouldn t it be better if we could write that piece of code just once and then reuse it in many applications? See Programming in Fortran 90/95, 1st or 2nd edition, Chapter

2 User-Defined Functions So, it d be nice to replace the code OU_opponent_score_sum = 0.0 DO game = first_game, number_of_games OU_opponent_score_sum = OU_opponent_score_sum + OU_opponent_score(game) END DO!! game = first_game, number_of_games OU_opponent_score_mean = OU_opponent_score_sum / number_of_games with calls to a function that would calculate the mean for any array: OU_opponent_score_mean = mean(ou_opponent_score_value, number_of_games) Obviously, the designers of Fortran 90 weren t able to anticipate the zillion things that we might need functions to do such as calculate the mean so there are no intrinsic functions to calculate things that are somewhat more application-specific. Instead, we as Fortran 90 programmers are going to have to define our own function to do it. A User-Defined Function for Mean REAL,DIMENSION(number_of_elements), INTENT(IN) :: array IF (number_of_elements < minimum_number_of_elements) THEN PRINT *, "ERROR: can t have an array ", "of length ", number_of_elements, ":" PRINT *, " it must have at least ", END IF!! (number_of_elements < min) Because we as users of Fortran 90 define this kind of function our own personal selves, this kind of function is referred to as a userdefined function. 3 4

3 array, length Start # elements < 1 Initialize sum Initialize index index <= # elts True Increase sum Increment index Flowchart for Mean Function Error message Divide sum Return Stop mean User-Defined Function Definitions REAL,DIMENSION(number_of_elements), INTENT(IN) :: array IF (number_of_elements < minimum_number_of_elements) THEN PRINT *, "ERROR: can t have an array ", "of length ", number_of_elements, ":" PRINT *, " it must have at least ", END IF!! (number_of_elements < min) In general, the definition of a user-defined function looks an awful lot like a program, except for the following things: 1. FUNCTION and END FUNCTION statements replace the PROGRAM and END PROGRAM statements of a program. 2. The FUNCTION statement begins with a return type. 3. At the end of the FUNCTION statement is a list of arguments, enclosed in parentheses and separated by commas. 4. In the declaration section of the function, each argument must be declared and have an extra attribute called an intent attribute. 5. In addition, the function may declare local named constants and local variables. 6. In the body of the function, the function name is used exactly as if it were a variable and must be assigned a value. 5 6

4 User-Defined Function Definitions: Declarations Are Valid Only Within the Unit Where They Occur REAL,DIMENSION(number_of_elements), INTENT(IN) :: array IF (number_of_elements < minimum_number_of_elements) THEN PRINT *, "ERROR: can t have an array ", "of length ", number_of_elements, ":" PRINT *, " it must have at least ", END IF!! (number_of_elements < min) A unit is either the main program or a function definition. (We ll see other kinds of units later.) The compiler treats each unit completely independently of the others. Most importantly, the declarations within a unit apply only to that unit, not to any others. For example, the declaration of initial sum in the function mean is visible only to the function mean and not to the main program or to any other function. 7 User-Defined Function Definitions: FUNCTION and END FUNCTION Statements REAL,DIMENSION(number_of_elements), INTENT(IN) :: array IF (number_of_elements < minimum_number_of_elements) THEN PRINT *, "ERROR: can t have an array ", "of length ", number_of_elements, ":" PRINT *, " it must have at least ", END IF!! (number_of_elements < min) In a function definition, the keyword PROGRAM is replaced by the keyword FUNCTION. So, a function definition has a FUNCTION statement and an END FUNCTION statement, which are analogous to a program s PROGRAM statement and END PROGRAM statement. 8

5 User-Defined Function Definitions: Return Type REAL,DIMENSION(number_of_elements), INTENT(IN) :: array IF (number_of_elements < minimum_number_of_elements) THEN PRINT *, "ERROR: can t have an array ", "of length ", number_of_elements, ":" PRINT *, " it must have at least ", END IF!! (number_of_elements < min) In the FUNCTION statement, immediately before the keyword FUNCTION is a data type. This data type specifies the return type, which is the data type of the value that the function will return. The return type must be a basic scalar type (e.g., INTEGER, REAL, LOGICAL, CHARACTER). Notice that the return type of the function is not declared in the traditional way, but is declared nonetheless. 9 User-Defined Function Definitions: List of Arguments REAL,DIMENSION(number_of_elements), INTENT(IN) :: array IF (number_of_elements < minimum_number_of_elements) THEN PRINT *, "ERROR: can t have an array ", "of length ", number_of_elements, ":" PRINT *, " it must have at least ", END IF!! (number_of_elements < min) At the end of the FUNCTION statement, immediately after the function name, is a list of arguments, enclosed in parentheses and separated by commas (just as in a mathematical function definition). The names of these arguments do not have to match the names of the arguments that are passed into the function by the program (or other function) that calls the function. They should be meaningful with respect to the function, not with respect to the program(s) that call the function. 10

6 User-Defined Function Definitions: INTENT Attribute REAL,DIMENSION(number_of_elements), INTENT(IN) :: array In the function s declaration section, each argument must be declared exactly as if it were a variable, except that it must have an additional intent attribute. The intent attribute gives the compiler important information about how the argument is to be used; specifically: whether the argument already has a value when it is passed into the function; whether the function will try to set or change the argument s value. 1. INTENT(IN): (a) the argument does already have a value when it is passed into the function; (b) the function will not try to change the argument s value. 2. INTENT(OUT): (a) the argument does not already have a value when it is passed into the function; (b) the function will set the argument s value. 3. INTENT(INOUT): (a) the argument may already have a value when it is passed into the function; (b) the function may change the argument s value. User-Defined Function Definitions: Argument Order for Arrays REAL,DIMENSION(number_of_elements), INTENT(IN) :: array When using an array argument, you must also use an argument that represents the length of the array. Not surprisingly, this argument should: 1. be of type INTEGER, and 2. have INTENT(IN), since the length of the array should not change inside the function. Notice that, in a sense, giving an argument the INTENT(IN) attribute indicates that the argument s value will not change within the function that is, the argument is constant with respect to the function (though not necessarily with respect to the main program)

7 User-Defined Function Definitions: Local Variables Named Constants REAL,DIMENSION(number_of_elements), INTENT(IN) :: array The function s declaration section may contain, in addition to declarations for the functions arguments, declarations of local named constants and local variables. These names that are valid ONLY within the function that is being defined. On the other hand, these same names can be used with totally different meanings by other functions (and by the calling program). Good programming style requires declaring: 1. arguments first, followed by 2. local named constants, followed by 3. local variables. User-Defined Function Definitions: Assigning a Return Value to the Return Variable REAL,DIMENSION(number_of_elements), INTENT(IN) :: array IF (number_of_elements < minimum_number_of_elements) THEN PRINT *, "ERROR: can t have an array ", "of length ", number_of_elements, ":" PRINT *, " it must have at least ", END IF!! (number_of_elements < min) In the body of the function, the name of the function is used exactly as if it were a variable, and it must be assigned the value that the function is to return. Therefore, it s referred to as the return variable. If the return variable is not assigned a value, the compiler gets upset

8 An Important Point About Declarations Inside Functions The following point is EXTREMELY important: The only symbolic names that a given unit (i.e., program unit or function unit) is aware of are those that are explicitly declared in the unit s declaration section. Thus, the unit is aware of: 1. its arguments (if it is a function); 2. its local named constants; 3. its local variables; 4. other functions that it has declared as EXTERNAL (described later). General Form of User-Defined Function Definitions returntype FUNCTION funcname ( arg1, arg2, ) arg1type, arg1attributes,intent( arg1intent ) :: arg1 arg2type, arg2attributes,intent( arg2intent ) :: arg2 localconst1type,parameter :: localconst1 localconst2type,parameter :: localconst2 localvar1type, localvar1attributes :: localvar1 localvar2type, localvar2attributes :: localvar2 [ function body: does stuff ] funcname = returnvalue END FUNCTION funcname The unit knows NOTHING AT ALL about variables declared inside any other unit. It isn t aware that they exist and cannot use them. Therefore, the ONLY way to send information from the program unit to a function unit, or from one function unit to another, is by passing arguments from the calling unit to the called unit

9 Using a User-Defined Function: Example PROGRAM mean_function_test INTEGER,PARAMETER :: lower_bound = 1 INTEGER,PARAMETER :: memory_success = 0 REAL,DIMENSION(:),ALLOCATABLE :: input_value INTEGER :: number_of_elements, index INTEGER :: allocate_status, deallocate_status REAL :: input_mean REAL,EXTERNAL :: mean PRINT *, "How many elements are in the input_value" PRINT *, " (at least ", lower_bound, ")?" READ *, number_of_elements IF (number_of_elements < lower_bound) THEN PRINT *, "Idiot! I said at least ", lower_bound, ")!" END IF!! (number_of_elements < lower_bound) ALLOCATE(input_value(number_of_elements), STAT=allocate_status) IF (allocate_status /= memory_success) THEN PRINT *, "ERROR: can t allocate a REAL array ", "of ", number_of_elements, " elements." END IF!! (allocate_status /= memory_success) PRINT *, "What are the ", number_of_elements, " elements of the input_value?" READ *, (input_value(index), index = lower_bound, number_of_elements) input_mean = mean(input_value, number_of_elements) PRINT *, "The mean of the ", number_of_elements, " elements is ", input_mean, "." DEALLOCATE(input_value, STAT=deallocate_status) IF (deallocate_status /= memory_success) THEN PRINT *, "ERROR: can t deallocate a REAL array ", "of ", number_of_elements, " elements." END IF!! (deallocate_status /= memory_success) END PROGRAM mean_function_test Using a User-Defined Function: Example Run % f95 -o meanfunctestall meanfunctestall.f90 % meanfunctestall How many elements are in the input_value (at least 1 )? 6 What are the 6 elements of the input_value? The mean of the 6 elements is

10 Jargon: Program Unit Function Unit In a Fortran 90 program, the set of statements from the PROGRAM statement through the END PROGRAM statement is referred to as the program unit. Similarly, for a particular user-defined function, the set of statements from the FUNCTION statement through the END FUNCTION statement is referred to as that particular function unit. Another User-Defined Function Example Here s a definition of a user-defined function. REAL FUNCTION cube_root (original_value) REAL,INTENT(IN) :: original_value REAL,PARAMETER :: cube_root_power = 1.0 / 3.0 cube_root = original_value ** cube_root_power END FUNCTION cube_root What can we say about this user-defined function? 1. Its name is cube root. 2. Its return type is REAL. 3. It has one argument, original value, whose type is REAL and whose intent is IN, so the argument already has a value when it is passed in, and the function will not change that value (that is, the function will treat the argument as if it were a named constant). 4. It has one local named constant, cube root power. 5. Its return variable is cube root. 6. It calculates and returns the cube root of the incoming argument. So, cube root calculates the cube root of a REAL argument and returns a REAL result whose value is the cube root of the argument. Does the name of a user-defined function have to be meaningful? Absolutely not; you could easily have a function named square root that always returns 12. But that d be REALLY REALLY DUMB, and you d get a VERY BAD GRADE

11 Using a User-Defined Function: Example #1 % cat cube_root_scalar.f90 PROGRAM cube_root_scalar INTEGER,PARAMETER :: number_of_values = 3 REAL :: input_value1, cube_root_value1 REAL :: input_value2, cube_root_value2 REAL :: input_value3, cube_root_value3 REAL,EXTERNAL :: cube_root! Function return type PRINT *, "What ", number_of_values, " real numbers would you like ", "the cube roots of?" READ *, input_value1, input_value2, input_value3 cube_root_value1 = cube_root(input_value1) cube_root_value2 = cube_root(input_value2) cube_root_value3 = cube_root(input_value3) PRINT *, "The cube root of ", input_value1, " is ", cube_root_value1, "." PRINT *, "The cube root of ", input_value2, " is ", cube_root_value2, "." PRINT *, "The cube root of ", input_value3, " is ", cube_root_value3, "." END PROGRAM cube_root_scalar The EXTERNAL Declaration PROGRAM cube_root_scalar REAL,EXTERNAL :: cube_root! Function return type END PROGRAM cube_root_scalar Notice this declaration: REAL,EXTERNAL :: cube root This declaration tells the compiler that there s a function named cube root with a return type of REAL, and that it s declared external to (outside of) the program unit that s calling the cube root function. REAL FUNCTION cube_root (original_value) REAL,INTENT(IN) :: original_value REAL,PARAMETER :: cube_root_power = 1.0 / 3.0 cube_root = original_value ** cube_root_power END FUNCTION cube_root % f95 -o cube_root_scalar cube_root_scalar.f90 % cube_root_scalar What 3 real numbers would you like the cube roots of? 1, 8, 100 The cube root of is The cube root of is The cube root of E+02 is

12 Actual Arguments Formal Arguments PROGRAM cube_root_scalar cube_root_value1 = cube_root(input_value1) cube_root_value2 = cube_root(input_value2) cube_root_value3 = cube_root(input_value3) END PROGRAM cube_root_scalar REAL FUNCTION cube_root (original_value) REAL,INTENT(IN) :: original_value END FUNCTION cube_root When we talk about the arguments of a function, we re actually talking about two very different kinds of arguments. The arguments that appear in the call to the function for example, input value1, input value2 and input value3, in the program fragment above are referred to as actual arguments, because they re the values that are actually getting passed to the function. On the other hand, the arguments that appear in the definition of the function for example, original value, in the function fragment above are referred to as formal arguments, because they re the names that are used in the formal definition of the function. Jargon: in Fortran 90, you ll sometimes hear formal arguments referred to as dummy arguments. User-Defined Function Example #2: Array % cat cube_root_array.f90 PROGRAM cube_root_array INTEGER,PARAMETER :: first_input = 1 INTEGER,PARAMETER :: number_of_inputs = 5 REAL,DIMENSION(number_of_inputs) :: input_value, cube_root_value INTEGER :: index REAL,EXTERNAL :: cube_root PRINT *, "What ", number_of_inputs, " real numbers" PRINT *, " would you like the cube roots of?" READ *, (input_value(index), index = first_input, number_of_inputs) DO index = first_input, number_of_inputs cube_root_value(index) = cube_root(input_value(index)) END DO!! index = first_input, number_of_inputs DO index = first_input, number_of_inputs PRINT *, "The cube root of ", input_value(index), " is ", cube_root_value(index), "." END DO!! index = first_input, number_of_inputs END PROGRAM cube_root_array REAL FUNCTION cube_root (original_value) REAL,INTENT(IN) :: original_value REAL,PARAMETER :: cube_root_power = 1.0 / 3.0 cube_root = original_value ** cube_root_power END FUNCTION cube_root % f95 -o cube_root_array cube_root_array.f90 % cube_root_array What 5 real numbers would you like the cube roots of? The cube root of is The cube root of is The cube root of E+02 is The cube root of E+02 is The cube root of E+02 is

1. User-Defined Functions & Subroutines Part 2 Outline

1. User-Defined Functions & Subroutines Part 2 Outline User-Defined Functions Subroutines Part 2 Outline 1. User-Defined Functions Subroutines Part 2 Outline 2. Argument Order When Passing Arrays 3. Code Reuse Is GOOD GOOD GOOD 4. Reusing User-Defined Functions

More information

User Defined Functions 1 Outline

User Defined Functions 1 Outline User Defined Functions 1 Outline 1. User Defined Functions 1 Outline 2. Standard Library Not Enough #1 3. Standard Library Not Enough #2 4. Calling a Function Instead 5. Why User-Defined Functions? 6.

More information

User Defined Functions 2 Outline

User Defined Functions 2 Outline User Defined Functions 2 Outline 1. User Defined Functions 2 Outline 2. Argument Order When Passing Arrays #1 3. Argument Order When Passing Arrays #1 4. Code Reuse Is GOOD GOOD GOOD #1 5. Code Reuse Is

More information

Subroutines and Functions

Subroutines and Functions Subroutines and Functions Procedures: Subroutines and Functions There are two types of procedures: SUBROUTINE: a parameterized named sequence of code which performs a specific task and can be invoked from

More information

Review More Arrays Modules Final Review

Review More Arrays Modules Final Review OUTLINE 1 REVIEW 2 MORE ARRAYS Using Arrays Why do we need dynamic arrays? Using Dynamic Arrays 3 MODULES Global Variables Interface Blocks Modular Programming 4 FINAL REVIEW THE STORY SO FAR... Create

More information

Review Functions Subroutines Flow Control Summary

Review Functions Subroutines Flow Control Summary OUTLINE 1 REVIEW 2 FUNCTIONS Why use functions How do they work 3 SUBROUTINES Why use subroutines? How do they work 4 FLOW CONTROL Logical Control Looping 5 SUMMARY OUTLINE 1 REVIEW 2 FUNCTIONS Why use

More information

Introduction to Fortran Programming. -Internal subprograms (1)-

Introduction to Fortran Programming. -Internal subprograms (1)- Introduction to Fortran Programming -Internal subprograms (1)- Subprograms Subprograms are used to split the program into separate smaller units. Internal subprogram is not an independent part of a program.

More information

FORTRAN 90: Functions, Modules, and Subroutines. Meteorology 227 Fall 2017

FORTRAN 90: Functions, Modules, and Subroutines. Meteorology 227 Fall 2017 FORTRAN 90: Functions, Modules, and Subroutines Meteorology 227 Fall 2017 Purpose First step in modular program design Cannot always anticipate all of the steps that will be needed to solve a problem Easier

More information

Intrinsic Functions Outline

Intrinsic Functions Outline Intrinsic Functions Outline 1. Intrinsic Functions Outline 2. Functions in Mathematics 3. Functions in Fortran 90 4. A Quick Look at ABS 5. Intrinsic Functions in Fortran 90 6. Math: Domain Range 7. Programming:

More information

Subroutines, Functions and Modules

Subroutines, Functions and Modules Subroutines, Functions and Modules Subdividing the Problem Most problems are thousands of lines of code. Few people can grasp all of the details. Good design principle: Exhibit the overall structure in

More information

Introduction to Modern Fortran

Introduction to Modern Fortran Introduction to Modern Fortran p. 1/?? Introduction to Modern Fortran Advanced Use Of Procedures Nick Maclaren nmm1@cam.ac.uk March 2014 Introduction to Modern Fortran p. 2/?? Summary We have omitted some

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s. Using Monte Carlo to Estimate π using Buffon s Needle Problem An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s. Here s the problem (in a simplified form). Suppose

More information

Lecture V: Introduction to parallel programming with Fortran coarrays

Lecture V: Introduction to parallel programming with Fortran coarrays Lecture V: Introduction to parallel programming with Fortran coarrays What is parallel computing? Serial computing Single processing unit (core) is used for solving a problem One task processed at a time

More information

Introduction to Fortran Programming. -External subprograms-

Introduction to Fortran Programming. -External subprograms- Introduction to Fortran Programming -External subprograms- Subprograms Subprograms are used to split a program into separate smaller units. Internal subprograms are dependent parts of a program. Fortran

More information

(Refer Slide Time 01:41 min)

(Refer Slide Time 01:41 min) Programming and Data Structure Dr. P.P.Chakraborty Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture # 03 C Programming - II We shall continue our study of

More information

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3 Programming in C main Level 2 Level 2 Level 2 Level 3 Level 3 1 Programmer-Defined Functions Modularize with building blocks of programs Divide and Conquer Construct a program from smaller pieces or components

More information

Computational Astrophysics AS 3013

Computational Astrophysics AS 3013 Computational Astrophysics AS 3013 Lecture 2: 1) F90 variable types 2) variable declaration 3) good programming style AS3013: F90 lecture2 1 Fortran 90 variable types integer whole numbers: 3, 244, -10,

More information

Computers in Engineering COMP 208. Subprograms. Subroutines. Subroutines Michael A. Hawker

Computers in Engineering COMP 208. Subprograms. Subroutines. Subroutines Michael A. Hawker Computers in Engineering COMP 208 Subroutines Michael A. Hawker Subprograms Functions are one type of subprogram in FORTRAN Another type of subprogram FORTRAN allows is called a subroutine There are many

More information

Programming Tips for Plugins

Programming Tips for Plugins Programming Tips for Plugins Chad Neufeld Centre for Computational Geostatistics Department of Civil & Environmental Engineering University of Alberta Working in a university based research environment

More information

Computers in Engineering. Subroutines Michael A. Hawker

Computers in Engineering. Subroutines Michael A. Hawker Computers in Engineering COMP 208 Subroutines Michael A. Hawker Subprograms Functions are one type of subprogram in FORTRAN Another type of subprogram FORTRAN allows is called a subroutine There are many

More information

Programming Languages

Programming Languages Programming Languages Tevfik Koşar Lecture - VIII February 9 th, 2006 1 Roadmap Allocation techniques Static Allocation Stack-based Allocation Heap-based Allocation Scope Rules Static Scopes Dynamic Scopes

More information

Subprograms. FORTRAN 77 Chapter 5. Subprograms. Subprograms. Subprograms. Function Subprograms 1/5/2014. Satish Chandra.

Subprograms. FORTRAN 77 Chapter 5. Subprograms. Subprograms. Subprograms. Function Subprograms 1/5/2014. Satish Chandra. FORTRAN 77 Chapter 5 Satish Chandra satish0402@gmail.com When a programs is more than a few hundred lines long, it gets hard to follow. Fortran codes that solve real research problems often have tens of

More information

Old Questions Name: a. if b. open c. output d. write e. do f. exit

Old Questions Name: a. if b. open c. output d. write e. do f. exit Old Questions Name: Part I. Multiple choice. One point each. 1. Which of the following is not a Fortran keyword? a. if b. open c. output d. write e. do f. exit 2. How many times will the code inside the

More information

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n) Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Module 10A Lecture - 20 What is a function?

More information

AN INTRODUCTION TO FORTRAN 90 LECTURE 2. Consider the following system of linear equations: a x + a x + a x = b

AN INTRODUCTION TO FORTRAN 90 LECTURE 2. Consider the following system of linear equations: a x + a x + a x = b AN INTRODUCTION TO FORTRAN 90 LECTURE 2 1. Fortran 90. Arrays, functions and subroutines. 2. Scientific plotting. Gnuplot 1 Each coefficient and variable is a scalar. Lengthy and cumbersome! Program Scalar

More information

7. Procedures and Structured Programming

7. Procedures and Structured Programming 7. Procedures and Structured Programming ONE BIG PROGRAM external procedure: separated small and reusable program units to conduct individual subtasks smaller main program Each program unit can be debugged

More information

2.2 Syntax Definition

2.2 Syntax Definition 42 CHAPTER 2. A SIMPLE SYNTAX-DIRECTED TRANSLATOR sequence of "three-address" instructions; a more complete example appears in Fig. 2.2. This form of intermediate code takes its name from instructions

More information

Static Methods. Why use methods?

Static Methods. Why use methods? Static Methods A method is just a collection of code. They are also called functions or procedures. It provides a way to break a larger program up into smaller, reusable chunks. This also has the benefit

More information

Allocating Storage for 1-Dimensional Arrays

Allocating Storage for 1-Dimensional Arrays Allocating Storage for 1-Dimensional Arrays Recall that if we know beforehand what size we want an array to be, then we allocate storage in the declaration statement, e.g., real, dimension (100 ) :: temperatures

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #43 Multidimensional Arrays In this video will look at multi-dimensional arrays. (Refer Slide Time: 00:03) In

More information

6.001 Notes: Section 6.1

6.001 Notes: Section 6.1 6.001 Notes: Section 6.1 Slide 6.1.1 When we first starting talking about Scheme expressions, you may recall we said that (almost) every Scheme expression had three components, a syntax (legal ways of

More information

(Refer Slide Time: 1:27)

(Refer Slide Time: 1:27) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 1 Introduction to Data Structures and Algorithms Welcome to data

More information

This wouldn t work without the previous declaration of X. This wouldn t work without the previous declaration of y

This wouldn t work without the previous declaration of X. This wouldn t work without the previous declaration of y Friends We want to explicitly grant access to a function that isn t a member of the current class/struct. This is accomplished by declaring that function (or an entire other struct) as friend inside the

More information

Python Intro GIS Week 1. Jake K. Carr

Python Intro GIS Week 1. Jake K. Carr GIS 5222 Week 1 Why Python It s simple and easy to learn It s free - open source! It s cross platform IT S expandable!! Why Python: Example Consider having to convert 1,000 shapefiles into feature classes

More information

ParaFEM Coding Standard for Fortran 90. Contents. 1.0 Introduction. 2.0 Documentation. 2.1 External Documentation

ParaFEM Coding Standard for Fortran 90. Contents. 1.0 Introduction. 2.0 Documentation. 2.1 External Documentation ParaFEM Coding Standard for Fortran 90 This standard has been prepared by Lee Margetts, Francisco Calvo and Vendel Szeremi at the University of Manchester. It is based on Version 1.1 of the European Standards

More information

CS : Programming for Non-Majors, Fall 2018 Programming Project #5: Big Statistics Due by 10:20am Wednesday November

CS : Programming for Non-Majors, Fall 2018 Programming Project #5: Big Statistics Due by 10:20am Wednesday November CS 1313 010: Programming for Non-Majors, Fall 2018 Programming Project #5: Big Statistics Due by 10:20am Wednesday November 7 2018 This fifth programming project will give you experience writing programs

More information

Merge Sort. Run time typically depends on: Insertion sort can be faster than merge sort. Fast, able to handle any data

Merge Sort. Run time typically depends on: Insertion sort can be faster than merge sort. Fast, able to handle any data Run time typically depends on: How long things take to set up How many operations there are in each step How many steps there are Insertion sort can be faster than merge sort One array, one operation per

More information

C Programming for Engineers Functions

C Programming for Engineers Functions C Programming for Engineers Functions ICEN 360 Spring 2017 Prof. Dola Saha 1 Introduction Real world problems are larger, more complex Top down approach Modularize divide and control Easier to track smaller

More information

Welcome. Modern Fortran (F77 to F90 and beyond) Virtual tutorial starts at BST

Welcome. Modern Fortran (F77 to F90 and beyond) Virtual tutorial starts at BST Welcome Modern Fortran (F77 to F90 and beyond) Virtual tutorial starts at 15.00 BST Modern Fortran: F77 to F90 and beyond Adrian Jackson adrianj@epcc.ed.ac.uk @adrianjhpc Fortran Ancient History (1967)

More information

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet. Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that

More information

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu 1 Subprograms CS 315 Programming Languages Pinar Duygulu Bilkent University Introduction 2 Two fundamental abstraction facilities Process abstraction Emphasized from early days Data abstraction Emphasized

More information

C++ Arrays and Vectors

C++ Arrays and Vectors C++ Arrays and Vectors Contents 1 Overview of Arrays and Vectors 2 2 Arrays 3 2.1 Declaring Arrays................................................. 3 2.2 Initializing Arrays................................................

More information

These are notes for the third lecture; if statements and loops.

These are notes for the third lecture; if statements and loops. These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern

More information

Arrays. 1. Derived Data Types Outline

Arrays. 1. Derived Data Types Outline Derived Data Types Outline 1. Derived Data Types Outline 2. Arrays 3. A Company and Its Employees 4. Multiple Employees 5. A New Data Type 6. Breaking Down a Derived Type Definition 7. Declaring an Instance

More information

Boolean Expressions. Is Equal and Is Not Equal

Boolean Expressions. Is Equal and Is Not Equal 3 MAKING CHOICES ow that we ve covered how to create constants and variables, you re ready to learn how to tell your computer to make choices. This chapter is about controlling the flow of a computer program

More information

Outline of Fortran 90 Topics

Outline of Fortran 90 Topics Outline of Fortran 90 Topics Overview of Computing Computer Organization Languages Problem Solving Data Fortran 90 Character Set Variables Basic Data Types Expressions Numeric Non-numeric Control Structures

More information

Appendix D. Fortran quick reference

Appendix D. Fortran quick reference Appendix D Fortran quick reference D.1 Fortran syntax... 315 D.2 Coarrays... 318 D.3 Fortran intrisic functions... D.4 History... 322 323 D.5 Further information... 324 Fortran 1 is the oldest high-level

More information

Trombone players produce different pitches partly by varying the length of a tube.

Trombone players produce different pitches partly by varying the length of a tube. Trombone players produce different pitches partly by varying the length of a tube. 7 Variables A variable is a connection between a name and a value.* That sounds simple enough, but some complexities arise

More information

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors Solution sheet 1 Introduction Please note that there can be other solutions than those listed in this document. This is a literate Haskell file which is available as PDF, as well as literate Haskell source

More information

COMP 250 Winter 2011 Reading: Java background January 5, 2011

COMP 250 Winter 2011 Reading: Java background January 5, 2011 Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP

More information

Introduction to Counting, Some Basic Principles

Introduction to Counting, Some Basic Principles Introduction to Counting, Some Basic Principles These are the class notes for week. Before we begin, let me just say something about the structure of the class notes. I wrote up all of these class notes

More information

Chapter Two Bonus Lesson: JavaDoc

Chapter Two Bonus Lesson: JavaDoc We ve already talked about adding simple comments to your source code. The JDK actually supports more meaningful comments as well. If you add specially-formatted comments, you can then use a tool called

More information

Fortran 90 Two Commonly Used Statements

Fortran 90 Two Commonly Used Statements Fortran 90 Two Commonly Used Statements 1. DO Loops (Compiled primarily from Hahn [1994]) Lab 6B BSYSE 512 Research and Teaching Methods The DO loop (or its equivalent) is one of the most powerful statements

More information

Chapter 1. Math review. 1.1 Some sets

Chapter 1. Math review. 1.1 Some sets Chapter 1 Math review This book assumes that you understood precalculus when you took it. So you used to know how to do things like factoring polynomials, solving high school geometry problems, using trigonometric

More information

Introduction to L A TEX for MCS-236

Introduction to L A TEX for MCS-236 Introduction to L A TEX for MCS-236 Max Hailperin, based on a version by Tom LoFaro September 14, 2011 1 Why L A TEX? L A TEX is a very strange document formatting system. Actually, it is a combination

More information

Mr G s Java Jive. #11: Formatting Numbers

Mr G s Java Jive. #11: Formatting Numbers Mr G s Java Jive #11: Formatting Numbers Now that we ve started using double values, we re bound to run into the question of just how many decimal places we want to show. This where we get to deal with

More information

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. Lesson 2 VARIABLES Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. WHAT ARE VARIABLES? When you input data (i.e. information) into a computer

More information

Introduction to Computer Science Unit 2. Notes

Introduction to Computer Science Unit 2. Notes Introduction to Computer Science Unit 2. Notes Name: Objectives: By the completion of this packet, students should be able to describe the difference between.java and.class files and the JVM. create and

More information

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows: Pointers and Arrays Part II We will continue with our discussion on the relationship between pointers and arrays, and in particular, discuss how arrays with dynamical length can be created at run-time

More information

UNIT V Sub u P b ro r g o r g a r m a s

UNIT V Sub u P b ro r g o r g a r m a s UNIT V SubPrograms Outline Subprograms Parameter Passing Parameter correspondence Main Issues when designing subroutine in programming languages Parameter passing techniques Characteristics of Subprogram

More information

Modern Fortran OO Features

Modern Fortran OO Features Modern Fortran OO Features Salvatore Filippone School of Aerospace, Transport and Manufacturing, salvatore.filippone@cranfield.ac.uk IT4I, Ostrava, April 2016 S. Filippone (SATM) Modern Fortran OO Features

More information

PROBLEM SOLVING WITH FORTRAN 90

PROBLEM SOLVING WITH FORTRAN 90 David R. Brooks PROBLEM SOLVING WITH FORTRAN 90 FOR SCIENTISTS AND ENGINEERS Springer Contents Preface v 1.1 Overview for Instructors v 1.1.1 The Case for Fortran 90 vi 1.1.2 Structure of the Text vii

More information

Statements 2. a operator= b a = a operator b

Statements 2. a operator= b a = a operator b Statements 2 Outline Note: i=i+1 is a valid statement. Don t confuse it with an equation i==i+1 which is always false for normal numbers. The statement i=i+1 is a very common idiom: it just increments

More information

SUBROUTINE subroutine-name (arg1, arg2,..., argn)

SUBROUTINE subroutine-name (arg1, arg2,..., argn) FORTRAN Subroutines Syntax Form 1 SUBROUTINE subroutine-name (arg1, arg2,..., argn) [specification part] [execution part] [subprogram part] subroutine-name Form 2 SUBROUTINE subroutine-name () [specification

More information

5.6.1 The Special Variable this

5.6.1 The Special Variable this ALTHOUGH THE BASIC IDEAS of object-oriented programming are reasonably simple and clear, they are subtle, and they take time to get used to And unfortunately, beyond the basic ideas there are a lot of

More information

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

More information

Our Strategy for Learning Fortran 90

Our Strategy for Learning Fortran 90 Our Strategy for Learning Fortran 90 We want to consider some computational problems which build in complexity. evaluating an integral solving nonlinear equations vector/matrix operations fitting data

More information

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist In Handout 28, the Guide to Inductive Proofs, we outlined a number of specifc issues and concepts to be mindful about when

More information

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU CSE101-lec#12 Designing Structured Programs Introduction to Functions Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU Outline Designing structured programs in C: Counter-controlled repetition

More information

User Defined Functions

User Defined Functions User Defined Functions CS 141 Lecture 4 Chapter 5 By Ziad Kobti 27/01/2003 (c) 2003 by Ziad Kobti 1 Outline Functions in C: Definition Function Prototype (signature) Function Definition (body/implementation)

More information

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really Week 9: Methods 1 We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really long and hard to read Sometimes you

More information

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Functions. Lecture 6 COP 3014 Spring February 11, 2018 Functions Lecture 6 COP 3014 Spring 2018 February 11, 2018 Functions A function is a reusable portion of a program, sometimes called a procedure or subroutine. Like a mini-program (or subprogram) in its

More information

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd El-Shorouk Academy Acad. Year : 2013 / 2014 High Institute of Computer Science & Information Technology Term : 1 st Year : 2 nd Computer Science Department Object Oriented Programming Section (1) Arrays

More information

Microsoft Excel Level 2

Microsoft Excel Level 2 Microsoft Excel Level 2 Table of Contents Chapter 1 Working with Excel Templates... 5 What is a Template?... 5 I. Opening a Template... 5 II. Using a Template... 5 III. Creating a Template... 6 Chapter

More information

University of Kelaniya Sri Lanka

University of Kelaniya Sri Lanka University of Kelaniya Sri Lanka Scope, Lifetime and Storage Class of a Variable COSC 12533/ COST 12533 SACHINTHA PITIGALA 2017 - Sachintha Pitigala < 1 What is Scope? Scope of Identifier: The scope of

More information

Boolean Expressions. Is Equal and Is Not Equal

Boolean Expressions. Is Equal and Is Not Equal 3 MAKING CHOICES Now that we ve covered how to create constants and variables, you re ready to learn how to tell your computer to make choices. This chapter is about controlling the flow of a computer

More information

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CHAPTER 4 FUNCTIONS. 4.1 Introduction CHAPTER 4 FUNCTIONS 4.1 Introduction Functions are the building blocks of C++ programs. Functions are also the executable segments in a program. The starting point for the execution of a program is main

More information

Binary Trees

Binary Trees Binary Trees 4-7-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? What is a Tree? You are all familiar with what

More information

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING CMPE13/L: INTRODUCTION TO PROGRAMMING IN C SPRING 2012 Lab 3 Matrix Math Introduction Reading In this lab you will write a

More information

Software Development. Modular Design and Algorithm Analysis

Software Development. Modular Design and Algorithm Analysis Software Development Modular Design and Algorithm Analysis Precondition and Postcondition To create a good algorithm, a programmer must be able to analyse a precondition (starting state) and a postcondition

More information

Storage and Sequence Association

Storage and Sequence Association 2 Section Storage and Sequence Association 1 1 HPF allows the mapping of variables across multiple processors in order to improve parallel 1 performance. FORTRAN and Fortran 0 both specify relationships

More information

C++ Reference NYU Digital Electronics Lab Fall 2016

C++ Reference NYU Digital Electronics Lab Fall 2016 C++ Reference NYU Digital Electronics Lab Fall 2016 Updated on August 24, 2016 This document outlines important information about the C++ programming language as it relates to NYU s Digital Electronics

More information

Limits. f(x) and lim. g(x) g(x)

Limits. f(x) and lim. g(x) g(x) Limits Limit Laws Suppose c is constant, n is a positive integer, and f() and g() both eist. Then,. [f() + g()] = f() + g() 2. [f() g()] = f() g() [ ] 3. [c f()] = c f() [ ] [ ] 4. [f() g()] = f() g()

More information

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5 C++ Data Types Contents 1 Simple C++ Data Types 2 2 Quick Note About Representations 3 3 Numeric Types 4 3.1 Integers (whole numbers)............................................ 4 3.2 Decimal Numbers.................................................

More information

Chapter 2: Programming Concepts

Chapter 2: Programming Concepts Chapter 2: Programming Concepts Objectives Students should Know the steps required to create programs using a programming language and related terminology. Be familiar with the basic structure of a Java

More information

Fortran 95/2003 Course

Fortran 95/2003 Course Fortran 95/2003 Course Procedures and Modules by Hartmut Häfner March 25, 2015 STEINBUCH CENTRE FOR COMPUTING - SCC KIT University of the State of Baden-Württemberg and National Laboratory of the Helmholtz

More information

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved. Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #44 Multidimensional Array and pointers In this video, we will look at the relation between Multi-dimensional

More information

AMath 483/583 Lecture 8

AMath 483/583 Lecture 8 AMath 483/583 Lecture 8 This lecture: Fortran subroutines and functions Arrays Dynamic memory Reading: class notes: Fortran Arrays class notes: Fortran Subroutines and Functions class notes: gfortran flags

More information

CS103 Spring 2018 Mathematical Vocabulary

CS103 Spring 2018 Mathematical Vocabulary CS103 Spring 2018 Mathematical Vocabulary You keep using that word. I do not think it means what you think it means. - Inigo Montoya, from The Princess Bride Consider the humble while loop in most programming

More information

Mobile App:IT. Methods & Classes

Mobile App:IT. Methods & Classes Mobile App:IT Methods & Classes WHAT IS A METHOD? - A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. -

More information

Introduction to Functions. Functions. Library Functions. Intrinsic Functions. Library Functions. Chapter 6 Fall 2015, CSUS. Chapter 6.

Introduction to Functions. Functions. Library Functions. Intrinsic Functions. Library Functions. Chapter 6 Fall 2015, CSUS. Chapter 6. s Introduction to s Chapter 6 Fall 2015, CSUS Chapter 6.1 Introduction to s Library s A function is a module that returns a value back to the part of the program that called it Found in practically all

More information

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1 CPE 101 mod/reusing slides from a UW course Overview (4) Lecture 4: Arithmetic Expressions Arithmetic expressions Integer and floating-point (double) types Unary and binary operators Precedence Associativity

More information

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections C++ Programming Chapter 6 Arrays and Vectors Yih-Peng Chiou Room 617, BL Building (02) 3366-3603 3603 ypchiou@cc.ee.ntu.edu.tw Photonic Modeling and Design Lab. Graduate Institute of Photonics and Optoelectronics

More information

More About WHILE Loops

More About WHILE Loops More About WHILE Loops http://people.sc.fsu.edu/ jburkardt/isc/week04 lecture 07.pdf... ISC3313: Introduction to Scientific Computing with C++ Summer Semester 2011... John Burkardt Department of Scientific

More information

Review of the C Programming Language for Principles of Operating Systems

Review of the C Programming Language for Principles of Operating Systems Review of the C Programming Language for Principles of Operating Systems Prof. James L. Frankel Harvard University Version of 7:26 PM 4-Sep-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights

More information

Introduction to Programming

Introduction to Programming Introduction to Programming session 9 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2010 These slides are created using Deitel s slides Sahrif University of Technology Outlines

More information

1. Character/String Data, Expressions & Intrinsic Functions. Numeric Representation of Non-numeric Values. (CHARACTER Data Type), Part 1

1. Character/String Data, Expressions & Intrinsic Functions. Numeric Representation of Non-numeric Values. (CHARACTER Data Type), Part 1 Character/String Data, Expressions Intrinsic Functions (CHARACTER Data Type), Part 1 1. Character/String Data, Expressions Intrinsic Functions (CHARACTER Data Type), Part 1 2. Numeric Representation of

More information

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the

More information