Intrinsic (build-in) Functions Examples of intrinsic functions: SQRT: y=sqrt(2.0) ABS: y=abs(x) SIN: y=sin(x) (x: radians) ATAN: pi=4.0*ATAN(1.0) When.

Slides:



Advertisements
Similar presentations
Lecture 07 – Iterating through a list of numbers.
Advertisements

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.
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
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.
ME1107 Computing Y Yan
Do Loop The syntax of DO loop: DO variable = initial_value, final_value[, increment] [statements] END DO Example: PROGRAM LINES ! Illustration of DO-loops.
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.
Arrays A variable: a named position of memory For example: x1, x2, x3……..x10 An array: a collection of variables of the same type that are referenced by.
Chapter 10 Modules and programming with subroutines.
Parameter Passing to Functions in C. C Parameter passing Review of by-value/by-reference.
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.
FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language.
LAB-12 2-D Array I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.
X = =2.67.
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.
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.
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.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Fortran 1- Basics Chapters 1-2 in your Fortran book.
E0001 Computers in Engineering Procedures: subprograms and functions.
CSEB114: Principle of programming
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
More While Loop Examples CS303E: Elements of Computers and Programming.
How to start Visual Studio 2008 or 2010 (command-line program)
CHAPTER 9 SUBPROGRAMS Subprograms are useful in the following cases: long complicated program  Subprograms make it readable and simple by dividing it.
11 - 2/4/2000AME 150 L Program Control Subroutines and Functions.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 1. Introduction.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 E0001 Computers in Engineering Built in Functions.
First steps Jordi Cortadella Department of Computer Science.
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.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
ME 142 Engineering Computation I Using Subroutines Effectively.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
Chapter 3 w Variables, constants, and calculations DIM statements - declaration temporary memory locations identifier, data type, scope data types - values.
Subroutines. Subroutine Subprograms Common features of Subroutines and functions –They perform particular tasks under the control of some other program.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
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.
More on F90 Outline: I.Parameter statements II.Comments III.Program layout-- execution part IV.if/then/else V.Case statements VI.do loops VII.goto VIII.Intrinsic.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
L AB 4 July 5th. F OR LOOP Exercise 1: Find two ways of “Summing all 1, 2, and 3 digit prime numbers.” Use for loop Hint: isprime, primes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
A Sample Program #include using namespace std; int main(void) { cout
Fundamental of Fortran Part 2 Dr.Entesar Ganash. 1. Program layout The general structure of a simple Fortran program is given as the following:
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Introduction to programming in java Lecture 21 Arrays – Part 1.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Algorithm & Flowchart.
1-1 Logic and Syntax A computer program is a solution to a problem.
Procedures and Modular Programming
PROGRAM CONTROL STRUCTURE
Subroutine Comp 208 Yi Lin.
Programming Right from the Start with Visual Basic .NET 1/e
Lecture 4 : January 2001 Dr. Andrew Paul Myers
Suppose I want to add all the even integers from 1 to 100 (inclusive)
mdul-cntns-sub-cmmnts2.f90
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
Presentation transcript:

Intrinsic (build-in) Functions Examples of intrinsic functions: SQRT: y=sqrt(2.0) ABS: y=abs(x) SIN: y=sin(x) (x: radians) ATAN: pi=4.0*ATAN(1.0) When do we need user-defined functions? not in the intrinsic function list long formulas repeatedly used formulas

Program structure (function) Input x X=0? Y=1 x=x*3.1416/180.0 y=sin(x)/x Output y end start T F Input x X=0? func1=1 x=x*3.1416/180.0 func1=sin(x)/x Output y end start T F end func1 y=func1(x) calculate sin(x)/x Single main programMain program with a subroutine

User-Defined Functions General Form [type] FUNCTION name (argument-list) [IMPLICIT NONE] type declarations executable statements END [FUNCTION [name]] PROGRAM EXAMPLE PRINT *, 'Input X, Y' READ *, X, Y r = RADIUS( X, Y ) PRINT *, 'Distance = ', r END PROGRAM EXAMPLE !========================= REAL FUNCTION RADIUS( A, B ) RADIUS = SQRT( A ** 2 + B ** 2 ) PRINT *, A,B END FUNCTION RADIUS On with line is the function called? Function name and type? How many arguments in the function?

User-Defined Functions Example Write a function which calculates the mean value of 10 numbers stored in a real type array. function name: mean (real type) argument list: x (a real type array, 10 elements) REAL FUNCTION mean(x) real x(10), sum sum=0 do i = 1, 10 sum = sum + x(i) end do mean = sum/10 END FUNCTION mean

User-Defined Functions Example Write a function which calculates the mean value of N numbers stored in a real type array. A value is assigned to N in the main program. function name: mean (real type) argument list: x (a real type array, N elements), N REAL FUNCTION mean(x, N) integer N real x(N), sum sum=0 do i = 1, N sum = sum + x(i) end do mean = sum/N END FUNCTION mean

Program structure (subroutine) Input x X=0? Y=1 x=x*3.1416/180.0 y=sin(x)/x Output y end start T F Input x X=0? Y=1 x=x*3.1416/180.0 y=sin(x)/x Output y end start T F end sub1(x,y) call sub1(x,y) calculate y=sin(x)/x) Single main programMain program with a subroutine

Subroutines General Form SUBROUTINE subroutine-name (argument-list) [IMPLICIT NONE] type declarations executable statements END [SUBROUTINE [name]] PROGRAM EXAMPLE PRINT *, 'Input X, Y' READ *, X, Y CALL DISTANCE( X, Y, RADIUS ) PRINT *, 'Distance = ', RADIUS END PROGRAM EXAMPLE !========================= SUBROUTINE DISTANCE( A, B, R ) R = SQRT( A ** 2 + B ** 2 ) END SUBROUTINE DISTANCE On which line is the subroutine called? subroutine name? argument list? intended data transfer direction? What is the main difference compared with functions? Subroutines are self-contained

Subroutines Example Write a subroutine which calculates the mean value of N numbers stored in a real type array. A value is assigned to N in the main program. subroutine name: get_mean argument list: x (a real type array, N elements), N, mean call get_mean(x, N, mean) subroutine get_mean(x, N, mean) integer N real x(N), sum, mean sum=0 do i = 1, N sum = sum + x(i) end do mean = sum/N END subroutine get_mean

Subroutines Exercise Write a subroutine which calculates the mean value and the standard deviation of N numbers stored in a real type array. A value is assigned to N in the main program. subroutine name: get_mean_std argument list: x (a real type array, N elements), N, mean, std call get_mean_std(x, N, mean, std) subroutine get_mean_std(x, N, mean, std) integer N real x(N), sum, mean, std sum=0 do i = 1, N sum = sum + x(i) end do mean = sum/N ( write the code for calculating std) END subroutine get_mean_std

The standard deviation The standard deviation: std = 0 Do i = 1, N std = std + (x(i)-mean)**2 loopexit std = SQRT(std/N) Flowchart: