FUNCTIONS IN FORTRAN For a complex program difficulties like writing it and debugging are encountered. These can be minimized by breaking main program.

Slides:



Advertisements
Similar presentations
Functions Function: The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use.
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
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 Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
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
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6: User-Defined Functions I
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Chapter 9 Introduction to Procedures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul -
FORTRAN PROGRAMMING BASICS MET 50. Programming Basics The basic layout of all programs is as follows (p.33) PROGRAM name = heading (i.e., title) Specifications.
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.
Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.
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
1 Arithmetic Expressions and Built in Functions 1.ARITHMETIC OPERATORS There are five basic arithmetic operations with Fortran. These are given in the.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
E0001 Computers in Engineering Procedures: subprograms and functions.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
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.
CHAPTER 9 SUBPROGRAMS Subprograms are useful in the following cases: long complicated program  Subprograms make it readable and simple by dividing it.
CPS120: Introduction to Computer Science Decision Making in Programs.
Week 3 Let's review! Fundamental data types List-directed input/output.
CPS120: Introduction to Computer Science Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 Remember: Examination is a chance not ability. By ILTAF MEHDI, IT LECTURER, MIHE, KATR-E-PARWAN BRANCH, KABUL.
User defined functions
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.
16/11/2006DSD,USIT,GGSIPU1 Packages The primary purpose of a package is to encapsulate elements that can be shared (globally) among two or more design.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CHAPTER 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
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.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
1 ICS103 Programming in C Lecture 8: Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
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.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
A First Book of ANSI C Fourth Edition
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
Chapter 7: Function.
Functions and Subroutines
Chapter 6: User-Defined Functions I
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
Lesson #6 Modular Programming and Functions.
Function Topic 4.
Lesson #6 Modular Programming and Functions.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
User-Defined Functions
Lesson #6 Modular Programming and Functions.
User Defined Functions
Chapter 6: User-Defined Functions I
Lesson #6 Modular Programming and Functions.
In C Programming Language
Functions Imran Rashid CTO at ManiWeber Technologies.
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
CPS125.
REPETITION Why Repetition?
Presentation transcript:

FUNCTIONS IN FORTRAN For a complex program difficulties like writing it and debugging are encountered. These can be minimized by breaking main program into parts called sub programs. Main program It is a full program in itself and can be executed itself. Sub program It is a small unit of program and cannot be executed itself.It is used by a main program or by other sub program. It is complete and independent in the sense (a)It is complete because it contains necessary type declaration ( such as REAL, INTEGER,DIMENSION etc. ) and use of library function & END statement. (b)It is independent because all variable names (except the function variable name) and all statement labels used are local to the subprogram and unknown outside. Thus the same variable names and statement labels may be used in the main(calling) program and diffehent subprograms. FUNCTIONS There are three types of functions (i) Library functions ( Built in functions ) (ii)Statement functions (iii)Function sub program (ii) & (iii) are user- defined functions.

TYPES OF SUB PROGRAM SUBPROGRAM SUBROUTINE FUNCTION Pre-Defined Built- in Fn./ Intrinsic Fn. / Library Fn. (i) User – Defined Fn. Function Subprogram / External Fn. / Multiline Fn. (iii Statement Function (ii)

(i)Pre-defined Built in function / Intrinsic function / Library function : These are pre- written programs by the manufacturers of compiler for some commonly used mathematical functions e.g. SQRT,MOD,SIN,COS etc. (ii)User- defined functions : These are of two types : (a) Statement function : It is defined by a single arithmetic or logical statement at the beginning of the main program or a sub program. Syntax : functionname (list of arguments ) =expression Rules: (i)List of arguments may be non-subscripted variable names called dummy arguments (ii)The expression on R H S may have some more variables in addition to list of arguments e.g. (i)AVE (A,B,C) =(A+B+C)/3.0 (ii) DIST(X,Y,Z) = SQRT (X*X+Y*Y+Z*Z) (iii) Use the statement function (ii) to evaluate expressiona (a) and (b): (iv) Use the statement function DIV (x,y) = (x+y) / (x-y) to evaluate (c),(d) (a) P = {(a+5) 2 + (b+7) 2 + (c+9) 2 } 3/2, (b) B= (u 2 +v 2 ) 1/2 / { (1+( p 2 + q 2 +r 2 ) 1/2 } (a)P = DIST (a+5.0, b+7.0, c+9.0) **3, (b) B=DIST (u,v,0.0) / (1.0 + DIST(p,q,r)) (c) A = (p 1/2 +5 )/ (p 1/2 -5), (d) b = 3 tan -1 ((1 +r ) / (1-r )) DIV ( x,y ) = ( x + y ) / (x – y) (c) A = DIV ( SQRT ( p),5.0) (d) b = 3.0 * ATAN (DIV ( 1.0, r ))

Illustration Of Statement Function Program : F( A,B ) = A**2 + B**2 Write (*,*) ‘Enter values of X and Y’ Read (*,*) X,Y C To calculate value of X 2 X2 = F( X,0.0) C To calculate value of Y 2 Y2 = F(Y,0.0) C To calculate value of ( X 2 +Y 2 ) X2 Y2 = F( X,Y) Write (*,*) X2, Y2, X2 Y2 Stop End

(b) Function subprogram / External function / Multiline function Syntax : Type Function name (list of arguments) ……………………………………………. name = expression RETURN END e.g. Integer function square (a, b, c) RULES : (i)Every Function subprogram returns a single value using the RETURN statement. (ii)The first statement in Function subprogram must always be the Function statement specifying type, name and dummy arguments.If type of Function name is declared then the Function name must be declared of same type in main or calling program. (iii)The Function name must appear at least once on LHS of an arithmetic expression (assignment statement) in its body. (iv)In MAIN PROGRAM it is called by assignment statement.When the name of a FUNCTION subprogram is encountered in source program, control is transferred to subprogram.The dummy arguments are replaced by the actual arguments in the body of FUNCTION subprogram and execution of subprogram is carried out.After execution is over the RETURN statement returns control to particular reference point in the calling program. (v)The list of dummy arguments are nonsubscriptedl variables and must match in number and order with calling program.Same variable names and statement numbers can be used in MAIN and SUBPROGRAM as these are local in function subprogram. (vi)RETURN is logical end of subprogram and returns the control at the calling point. (vii) Last statement END is physical end of the subprogram.

For Example : Calling Program : VAR = function name (a, b, c) ………………………………… END Function subprogram : function name (x, y, z ) ………………….. name = expression Return End

Determine Factorial of a number using function subprogram C Main Program Integer FACT Write(*,*) ‘Enter the number’ Read(*,*) N IF ( N.LT.0) THEN WRITE (*,*) ‘Factorial of a NEGATIVE NUMBER IS NOT DEFINED’ ELSE I = FACT ( N ) WRITE (*,*) N, I ENDIF STOP END C Function subprogram INTEGER FUNCTION FACT (K) IF ( K.EQ.O) THEN FACT = 1 RETURN ELSE FACT = 1 DO 15 I = 1,K FACT = FACT * I 15 CONTINUE ENDIF RETURN END

Compute n C r = n! / ( r! (n-r )! ) using function subprogram C Main Program Integer R, FACT Write(*,*) ‘Enter N and R’ Read(*,*) N, R I1 = FACT (N) I2 = FACT (R) I3 = FACT (N-R ) NCR = I1/ (I2 * I3 ) WRITE (*,*) N,R,NCR STOP END C Function Subprogram INTEGER FUNCTION FACT (K) IF ( K.EQ.O) THEN FACT = 1 RETURN ELSE FACT = 1 DO 15 I = 1,K FACT = FACT * I 15 CONTINUE ENDIF RETURN END

Write Function Subprogram to find the largest of three numbers Function Big (A,B,C) Big =A If ( Big.LT.B ) Big = B If ( Big.LT.C ) Big = C Return End

SUBROUTINE : SYNTAX : SUBROUTINE name of subroutine ( list of arguments ) ………………………………………………. Return End For calling a SUBROUTINE subprogram CALL statement is used in Main Program. i.e. in Main program CALL name of subroutine ( list of arguments ) RULES : (i)It is also a complete and independent program. (ii)The name of SUBROUTINE subprogram does not have a value. So CALL statement is required in Main program for using it. (iii)The communication between Main program and SUBROUTINE subprogram is only transmitted through parameters and not through its name. (iv)The execution of CALL transfers the control to SUBROUTINE subprogram.The dummy arguments are replaced by the actual arguments in the body of SUBROUTINE subprogram and execution of subprogram is carried out.After execution is over the RETURN statement transfers control back to the statement immediately following the CALL statement in the calling program.

EXAMPLE : Multiplication of two numbers using SUBROUTINE SUBPROGRAM C MAIN PROGRAM READ ( *, * ) IP, IQ CALL MUL ( IP, IQ, IT ) WRITE ( *,* ) IT END C SUBROUTINE SUBPROGRAM SUBROUTINE MUL ( IA, IB, IC ) IC = IA * IB RETURN END

DIFFERENCE BETWEEN FUNCTION SUBPROGRAM FUNCTION SUBPROGRAM 1.It can return only one value to calling prog. 2.It returns value through its name. 3.It has a type associated with its name which identifies type of value returned by it. 4.There must be at least one argument in the dummy list. 5. It is called by refering its name. 6. After execution of RETURN or END statement, the control is transferred back to the reference point in the calling prog. or subprog. SUBPROGRAM AND SUBROUTINE SUBROUTINE SUBPROGRAM It can return more than one value to calling prog. It returns value(s) through its arguments. It has no type associated with its name and values returned can be of diff. types identified by type of arguments returning values to the calling prog. There is no restriction i. e. it can be without any arguments. It is called by a CALL statement. After execution of RETURN or END statement, the control is transferred back to the statement immediately following the CALL statement in the calling prog. or subprog.

ADVANTAGES AND PURPOSE OF BREAKING A PROGRAM INTO SUBPROGRAMS : 1.Repetition : A subprogram may be used to avoid rewriting the same set of statements within the same program. 2.Universal Use : A subprogram written once can be used in more than one program and by users other than its developer. 3.Modularity : A complex program can be solved by modular approaches using the subprogram. 4.Team Work : A large program can be divided into different subprograms which can be written by the group of persons. 5.Debugging : A subprogram makes it easy to debug ( i.e. find errors ) in the program.