CEE15 Introduction to Computing for Civil Engineers 7

CEE15 Introduction to Computing for Civil Engineers 7 -...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CEE15 Introduction to Computing for Civil Engineers 75 Chapter 7 Subprograms 7.1 Designing Functions Syntax type FUNCTION function-name (arg1, arg2, . .., argn) IMPLICIT NONE [specification part] [execution part] [subprogram part] END FUNCTION function-name § The first line of a function starts with the keyword FUNCTION . Before FUNCTION , the type gives the type of the function value ( i.e. , INTEGER , REAL , LOGICAL and CHARACTER ) and after FUNCTION is the name you assign to that function. § Following the function-name , there is a pair of parenthesis in which a number of arguments arg1 , arg2 , . .., argn are separated with commas. These arguments are referred to as formal arguments . Formal arguments must be variable names and cannot be expressions. Examples: § The following is a function called Factorial . It takes only one formal argument n and returns an INTEGER as its function value. INTEGER FUNCTION Factorial(n) § The following is a function called TestSomething . It takes three formal arguments a , b and c , and returns a LOGICAL value ( i.e. , .TRUE. or .FALSE. ) as its function value. LOGICAL FUNCTION TestSomething(a, b, c) § A function could be ended with END , or END FUNCTION, or END FUNCTION followed by the name of that function. § Between FUNCTION and END FUNCTION , there are the IMPLICIT NONE , specification part , execution part subprogram part . These are exactly identical to that of a PROGRAM . § If a function does not need any formal argument, it can be written as type FUNCTION function-name () IMPLICIT NONE [specification part] [execution part] [subprogram part] END FUNCTION function-name where arg1 , arg2 , . .., argn are left out. But, the pair of parenthesis must be there. Semantics § A function is a self-contained unit that receives some "input" from the outside via its formal arguments, does some computations, and then returns the result with the name of the function. § Thus, since the function returns its result via the name of the function, somewhere in the function there must exist one or more assignment statements like the following:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CEE15 Introduction to Computing for Civil Engineers 76 function-name = expression where the result of expression is stored to the name of the function. A function receives its input values from formal arguments, does computations, and saves the result in its name. When the control of execution reaches END FUNCTION , the value stored in the name of the function is returned as the function value. § Any statements that can be used in PROGRAM can also be used in a FUNCTION . Examples: § The following function has a name Sum and three formal arguments a , b and c . It returns an INTEGER function value. The INTEGER part indicates that the function takes its input value from its three formal argument. Then, the function uses the value of these formal arguments to compute the sum and stores in Sum , the name of the function. Since the next statement is END FUNCTION , the function returns the value stored in Sum .
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 18

CEE15 Introduction to Computing for Civil Engineers 7 -...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online