Week 3 Let's review! Fundamental data types List-directed input/output.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 6: User-Defined Functions I
Summary of previous weeks Introduction to Scientific & Engineering Computing.
Program Design and Development
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Fortran 9x HTML version. New F90 features Free source form Modules User-defined data types and operators Generic user-defined procedures Interface blocks.
6 April, 2000 CS1001 Lecture 15 EXAM1 - results SUBPROGRAM - - revisit FUNCTION.
27 March, 2000 CS1001 Lecture 16 FUNCTIONS SUBROUTINES SCOPE MODULES EXTERNAL SUBPROGRAMS.
Basic Building Blocks In all ways of life, the easiest way to solve most problems is to break them down into smaller sub_problems and then to deal with.
Chapter 10 Modules and programming with subroutines.
Chapter 9 Modules and Programming with Functions.
Chapter 6: User-Defined Functions I
6 April, 2000 CS1001 Lecture 13 PRACTICE EXAM - revisit SUBPROGRAM FUNCTION.
1 BIL106E Introduction to Scientific & Engineering Computing Organizational matters Fortran 90 ( subset F ): Basics Example programs in detail.
Summary of previous weeks BIL 102 Introduction to Scientific & Engineering Computing.
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.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Fortran: Specification Statements Session Six ICoCSIS.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Fortran: Program Units and Procedures Session Four ICoCSIS.
Program readformat integer::n1,n2,n3 real::x,y,z print*,"Please enter " read"(3i3)",n1,n2,n3 print*,"The format for read this data is 3i3" print*,"n1=",n1,"n2=",n2,"n3=",n3.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Week 8. Today’s program n New stuff: –Recursion in F –Procedures as arguments –User-defined data types n Quiz #3.
Chapter 6: User-Defined Functions
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.
1 Week 2 n Organizational matters n Fortran 90 (subset F): Basics n Example programs in detail.
Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:
Module and Data Sharing. Programming in the Large Software, in general, is large having multiple units Multiple units designed and developed independently.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Basic Building Blocks In all ways of life, the easiest way to solve most problems is to break them down into smaller sub_problems and then to deal with.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
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.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 9 Functions Dept of Computer Engineering Khon Kaen University.
FUNCTIONS IN FORTRAN For a complex program difficulties like writing it and debugging are encountered. These can be minimized by breaking main program.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
Chapter 3: User-Defined Functions I
DATA HANDLING WEEK 3. n There are two fundamental types of numbers in both mathematics and programming, which may have either a fractional part or not.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Definition of the Programming Language CPRL
Chapter 9: Value-Returning Functions
Chapter 6: User-Defined Functions I
-Neelima Singh PGT(CS) KV Sec-3 Rohini
BASIC ELEMENTS OF A COMPUTER PROGRAM
Introduction to Computer Science / Procedural – 67130
Procedures and Modular Programming
Organization of Programming Languages
User-Defined Functions
Formatted and Unformatted Input/Output Functions
Functions Declarations CSCI 230
Summary BIL 106 Introduction to Scientific & Engineering Computing.
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Chapter 6: User-Defined Functions I
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
Class code for pythonroom.com cchsp2cs
CPS125.
Top-Down Design with Functions
Presentation transcript:

Week 3 Let's review! Fundamental data types List-directed input/output

Fundamental types of numbers n Integers  Whole numbers (positive/negative/zero)  Examples:  Typical range on a 32-bit computer -2 x 10 9 to +2 x 10 9

Fundamental types of numbers n Reals +/- xxx.yyyyy xxx integer part yyyyy fractional part n A better representation:  Sign:+/-  Mantissa:a fraction between 0.1 and 1.0  Exponent:x 10 e x or e-6

real and integer variables n Variable declaration: type :: name type :: name1, name2, … n integer :: a, b, c n real :: x, y, z

Arithmetic expressions n A combination of variables, constants, operators, parentheses… (4*Pi*Radius * * )/2.3

Assignment name = expression replace the content of the variable name with the result of the expression

List-directed input and output n read *, var_1, var_2, …  only variables! n print *, item_1, item_2, …  variables, constants, expressions, … n Value separators:  Comma (,)  Space  Slash (/)  End-of-line

List-directed input and output n Two consecutive commas:  a null value is read  the value of the variable is not set to zero, simply its value is not changed! n If the terminating character is a slash then no more dataitems are read; processing of the input statement is ended n Example

Character data n A B C D E F G H I J K L M N O P Q R S T U W X Y Z a b c d e f g h i j k l m n o p q r s t u w x y z lj = + - * / ( ),. ' : ! " % & ; ? $ Declaration: character (len=length) :: name1, name2, … character (len=6) :: a, b, c character (len=10*3):: a character (len=10) :: b Assignment a = "What a lovely afternoon!" a will have the value of "What a lovely afternoon! ljljljljljlj " b = a b will have the value of "What a lov"

Character data n Concatenation:  Operator // n Example: character (len=10) a, b, c a = "James" b = "Bond" c = trim(a)//"ž"//trim(b) c will have the value of "James Bond"

Named constants type, parameter :: name1=constant_expression1, … real, parameter :: pi= , pi_by_2 = pi/2.0 integer, parameter :: max_lines = 200

Now, some new stuff... Basic Building Blocks

Procedures n Divide and rule! Main program Procedure 1 Procedure 2 Procedure 3

Programs and modules n Main program unit program name use statements. Specification statements. Executable statements. end program name

Programs and modules n Module program unit module name use statements. Specification statements. contains Procedure definitions. end module name

Procedures n Divide and rule! Main program Procedure 1 Procedure 2 Procedure 3

Procedures n Procedures - origin  “Write your own” (homemade)  Intrinsic (built-in, comes with F ) sin(x), cos(x), abs(x), …  Written by someone else (libraries) n Procedures (subprograms) - form  Functions  Subroutines

Procedures n The main program amd any subprogram need never be aware of the internal details of any other program or subprogram! Main program Procedure 1 Procedure 2 Procedure 3

Functions function cube_root result(root) ! A function to calculate the cube root of a positive real number ! Dummy argument declaration real, intent(in) :: x ! Result variable declaration real :: root ! Local variable declaration real :: log_x ! Calculate cube root by using logs log_x = log(x) root = exp(log_x/3.0) end function cube_root

Functions function name (d1, d2, …) result (result_name) Specifications part Execution part end function name n Variables  Internal (local) variables  Result variable (keyword result )  Dummy argument (keyword intent(in) ) attribute

Functions n No side effects! Restrictions on the statements that can appear within a function

Subroutines subroutine roots (x, square_root, cube_root, fourth_root, & fifth_root) ! Subroutine to calculate various roots of positive real ! Number supplied as the first argument, and return them in ! the second to fifth arguments ! Dummy argument declarations real, intent(in) :: x real, intent(out) :: square_root, cube_root, & fourth_root, fifth_root ! Local variable declaration real :: log_x ! Calculate square root using intrinsic sqrt square_root = sqrt(x) ! Calculate other roots by using logs log_x = log(x) cube_root = exp(log_x/3.0) fourth_root = exp(log_x/4.0) fifth_root = exp(log_x/5.0) end subroutine roots

Subroutines call name (arg1, arg2, …) n intent(in), intent(out), intent(inout)

Arguments n Actual arguments in the calling program n Dummy arguments in the subroutine or function n The order and types of the actual arguments must correspond exactly with the order and types of the corresponding dummy arguments

Objects n Local variables vs. global variables private vs. public objects

Saving the values of local objects n Local entities within a procedure are not accessible from outside that procedure n Once an exit has been made, they cease to exist n If you want their values to ‘survive’ between calls, use real, save :: list of real variables

Homework n Due on March 8 n from Ellis & Philips  4.3  4.8  4.9  4.11