Download presentation
Presentation is loading. Please wait.
Published byGrant White Modified over 9 years ago
1
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.
2
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)
3
(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 ))
4
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
5
(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.
6
For Example : Calling Program : VAR = function name (a, b, c) ………………………………… END Function subprogram : function name (x, y, z ) ………………….. name = expression Return End
7
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
8
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
9
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
10
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.
11
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
12
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.
13
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.