Title: FUNCTIONS IN FORTRAN
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
- It is complete because it contains necessary type
declaration ( such as REAL, INTEGER,DIMENSION
etc. ) and use of library function END
statement. - 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 )
- Statement functions
- Function sub program
- (ii) (iii) are user- defined
functions.
2SUBPROGRAM
SUBROUTINE
FUNCTION
User Defined Fn.
Pre-Defined Built- in Fn./ Intrinsic Fn. /
Library Fn.
(i)
Function Subprogram / External Fn. /
Multiline Fn.
Statement Function
(ii)
(iii
3- 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. - 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
- List of arguments may be non-subscripted variable
names called dummy arguments - The expression on R H S may have some more
variables in addition to list of arguments - e.g.
- AVE (A,B,C) (ABC)/3.0 (ii) DIST(X,Y,Z)
SQRT (XXYYZZ) - (iii) Use the statement function
(ii) to evaluate expressiona (a) and (b) - (iv) Use the statement function DIV (x,y) (xy)
/ (x-y) to evaluate (c),(d) - (a) P (a5)2 (b7)2 (c9)2
3/2 , (b) B (u2v2)1/2 / (1( p2 q2
r2 )1/2 - P DIST (a5.0, b7.0, c9.0) 3 , (b)
BDIST (u,v,0.0) / (1.0 DIST(p,q,r)) - (c) A (p1/2 5 )/ (p1/2-5) ,
(d) b 3 tan-1 ((1 r )
/ (1-r ))
4- Illustration Of Statement Function Program
- F( A,B ) A2 B2
- Write (,) Enter values of X and Y
- Read (,) X,Y
- C To calculate value of X2
- X2 F( X,0.0)
- C To calculate value of Y2
- Y2 F(Y,0.0)
- C To calculate value of ( X2 Y2 )
- 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
- Every Function subprogram returns a single value
using the RETURN statement. - 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. - The Function name must appear at least once on
LHS of an arithmetic expression
(assignment statement) in its body. - 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. - 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 . - RETURN is logical end of subprogram and returns
the control at the calling point. - 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
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
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
- It is also a complete and independent program.
- The name of SUBROUTINE subprogram does not have
a value. So CALL statement is required in Main
program for using it. - The communication between Main program and
SUBROUTINE subprogram is only transmitted through
parameters and not through its name. - 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 - Repetition A subprogram may be used to avoid
rewriting the same set of statements within the
same program. - Universal Use A subprogram written once can be
used in more than one program and by users other
than its developer. - Modularity A complex program can be solved by
modular approaches using the subprogram. - Team Work A large program can be divided into
different subprograms which can be written by the
group of persons. - Debugging A subprogram makes it easy to debug
( i.e. find errors ) in the program.