ICS 101 Introduction to Computer Programming LAB 6 - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

ICS 101 Introduction to Computer Programming LAB 6

Description:

Function Subprogram is the description of a function consisting of several statements. The subprogram (in the case of function) computes a single value and stores that ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 24
Provided by: ics78
Category:

less

Transcript and Presenter's Notes

Title: ICS 101 Introduction to Computer Programming LAB 6


1
ICS 101 Introduction toComputer ProgrammingLAB
6
  • Zeehasham Rasheed
  • hasham_at_ccse.kfupm.edu.sa
  • Information Computer Science Department King
    Fahd University of Petroleum Minerals

2
Lab 6
  • Review of Functions
  • IF Statement with Functions
  • Special Cases of Functions
  • Intrinsic Function
  • Statement Function
  • Example
  • Exercises

3
Review of Functions
  • Already discussed in Lab 5, Review following lab

4
Function Sub-programs
  • Function Subprogram is the description of a
    function consisting of several statements.
  • The subprogram (in the case of function) computes
    a single value and stores that value in the
    function name.
  • A function subprogram consists of a function
    header and a function body

5
Function Sub-programs (Contd)
  • Function Header
  • Format
  • type FUNCTION fname (a list of arguments)
  • where
  • type is the type for the function name i.e. REAL,
    INTEGER
  • fname is the name of the function (6 variable)
  • a list of arguments is the optional list of
    arguments
  • if the type of the function is not specified, the
    function type is assumed as either INTEGER or
    REAL.
  • Rules that apply to naming a variable also apply
    for function name
  • If there are no arguments to a function, then the
    empty parentheses () appear with the function
    name

6
Function Sub-programs (Contd)
  • Function Body
  • Format
  • The function Body is similar to a FORTRAN program
  • Must end with END statement
  • The Return statement must appear in the function
    body atleast once.

7
Function Sub-programs (Contd)
  • General form of the function
  • TYPE FUNCTION FNAME ( A LIST OF DUMMY ARGUMENTS)
  • DECLARATION OF DUMMY ARGUMENTS AND VARIABLES TO
    BE USED IN THE FUNCTION
  • EXECUTABLE STATEMENTS
  • ..
  • FNAME EXPRESSION
  • ..
  • ..
  • RETURN
  • END

8
Function Sub-programs (Contd)
  • Example To compute Volume of a sphere with
    radius RADIUS
  • REAL FUNCTION VOLUME (RADIUS)
  • REAL RADIUS, PI
  • PI 22.0/7
  • VOLUME 4.0 / 3.0 PI RADUIS 3
  • RETURN
  • END

9
Function Call and Rules
  • Actual and dummy arguments must match in type,
    order and number. The names of these arguments
    may or may not be same.
  • Actual Arguments ( that is appear in a calling
    segment) can be expressions, constants or
    variable names.
  • F(AB)
  • ORDER (3,2,4)
  • ORDER (3,42, 99)
  • F(A)
  • BUT Dummy arguments must be variable names and
    should never be expressions or constants.

10
Function Call and Rules (Contd)
  • The type of the function name must be the same in
    both the calling program and the function
    description
  • The result from the function subprogram, to be
    returned to the calling program, should be stored
    in the function name.
  • Atleast one return statement in each subprogram
    should be there.
  • A function may be placed either before or after
    the main program, not in the middle.
  • A Fortran function cannot call itself.
  • A function is called or invoke in a main program
    or in other subprogram as part of an expression

11
Example
  • C Main program
  • INTEGER X, Y, Z, SUM
  • READ, X, Y, Z
  • PRINT, SUM(X, Y, Z)
  • END
  • C Function Subprogram
  • INTEGER FUNCTION SUM(A, B, C)
  • INTEGER A, B, C
  • SUM A B C
  • RETURN
  • END

12
IF Statement with Functions
  • Write a program, which takes two digit number as
    input and reverse the digits. Implement it with
    the help of function and also write a conditional
    construct which takes cares of condition that
    only two digit positive number is taken as input.
    Also function should return an error code -1 if
    the argument is not two digit number.

13
  • INTEGER FUNCTION RVSNUM(NUMBER)
  • INTEGER NUMBER, RDIGIT, LDIGIT
  • IF(NUMBER .LT. 10 .OR. NUMBER .GT. 99 ) THEN
  • RVSNUM -1
  • RETURN
  • ENDIF
  • LDIGIT NUMBER / 10
  • RDIGIT NUMBER - LDIGIT 10
  • RVSNUM RDIGIT 10 LDIGIT
  • RETURN
  • END

C MAIN PROGRAM INTEGER NUMBER, RVSNUM,
RNUM READ, NUMBER RNUM RVSNUM(NUMBER) IF(RNU
M .EQ. -1) THEN PRINT, 'INPUT ERROR
',NUMBER ELSE PRINT, 'ORIGINAL NUMBER IS ',
NUMBER PRINT, 'REVERSED NUMBER IS ',
RNUM ENDIF END
14
Special Cases of Functions
  • There are special cases of functions that do not
    require subprogram description.
  • Intrinsic (Built-in) Functions
  • Statement Function

15
Intrinsic Functions
  • These are predefined functions that are available
    from the FORTRAN language.
  • They are mostly mathematic functions
  • Example
  • SQRT(X) Square root of real number X, return
    REAL
  • ABS(X) Absolute value of X, return integer
  • SIN(X) Sine of angle X (angle is in radians).
    return REAL
  • EXP(X) e raised to the power X, return REAL
  • LOG(X) Natural Log of real number X, return REAL
  • LOG10(X) Log of real number X with base 10,
    return REAL
  • MOD(M,N) Remainder of M/N, integer argument,
    return integer

16
Statement Functions
  • Functions that can be write in a single
    statements
  • They are alternate to the function subprogram
  • A statement function is defined in the beginning
    of a program but after declaration statements
  • As a non-executable statement, it should appear
    before any executable statement.
  • General Form
  • fname ( a list of arguments, which should
    declared previously) expression
  • fname is the name of the functions
  • a list of arguments is the optional list of dummy
    arguments
  • expression computes the function value
  • One can note that type is not declared here
  • Type of fname should be declared just like other
    variables in the argument list or it will be
    implicitly defined

17
Statement Functions (Contd)
  • Example
  • Write a statement function to compute the area of
    a triangle, given its two sides and an angle
  • REAL AREA, SIDE1, SIDE2, ANGLE
  • AREA(SIDE1, SIDE2, ANGLE) 0.5 SIDE1
    SIDE2 SIN (ANGLE)
  • Write a statement function to compute the total
    number of seconds, given the time in hours,
    minutes and seconds
  • REAL TOTSEC, HOUR, MINUTE, SECONDS
  • TOTSEC (HOUR, MINUTE, SECOND) 3600 HOUR
    60 MINUTE SECOND

18
Statement Functions (Contd)
  • Write a program which computes the function F(X,
    Y) 3 X 2 5 X Y
  • REAL F, X, Y
  • F(X,Y) 3 X 2 5 X Y
  • PRINT, 'Input two real numbers'
  • READ, X, Y
  • PRINT, 'Output of function 3 X 2 5 X
    Y ', F(X,Y)
  • PRINT, 'INPUTS are ', X, ' and ', Y
  • END

19
Statement Functions (Contd)
  • Syntax error will occur if used at somewhere else
    in the program
  • Proper placement is necessary
  • If type is not mentioned in the declaration of
    the function name, implicit type will be assumed
    depending on the name of the function name (same
    case as in variable)

20
Exercise
  • With the help of the INTRINSIC function, find the
    square root of the positive real. With the help
    of conditional construct be sure to input the
    positive number.

21
Exercise
  • With the help of the INTRINSIC function, find the
    logarithm to the base 10 of the positive real.
    With the help of conditional construct be sure to
    input the positive number.

22
Exercise
  • Rewrite the following function in terms of
    statement function
  • REAL FUNCTION AREA(CIRCUM)
  • REAL CIRCUM, RADIUS, PI
  • PI 3.14159
  • RADIUS CIRCUM/(2.0PI)
  • AREA RADIUS 2 PI
  • RETURN
  • END

23
Exercise
  • Implement and Tell me the Flow of the following
    program
  • INTEGER FUNCTION MAXA(M,N)
  • INTEGER M, N
  • IF ( M .GT. N) THEN
  • MAXA M
  • ELSE
  • MAXA N
  • ENDIF
  • RETURN
  • END
  • INTEGER NUM1, NUM2, MAXA, ANS
  • PRINT, ' INPUT TWO NUMBERS WHICH ARE INTEGER '
  • READ, NUM1, NUM2
  • IF (NUM1 .NE. NUM2) THEN
  • ANS MAXA(NUM1,NUM2)
  • PRINT, ' Maximum number is ', ANS
Write a Comment
User Comments (0)
About PowerShow.com