CE 311 K - Introduction to Computer Methods

Lab 8 - Functions


Visual Basic programs are comprised of one or modules or procedures.  InVB, these procedures are either "sub-procedures" or "functions".  Each procedure is a self-contained block of code, that performs a specific task.  Often the algorithm to solve a problem is more complex than those we have seen and the programmer must break up the problem into subproblems to develop the program solution.  In attempting to solve a subproblem at one level, we introduce new subproblems at lower levels.  This process, called top-down design, proceeds from the original problem at the top level to the subproblems at each lower level.  The process of splitting a problem into its related subproblems is analogous to the process of refining an algorithm.  One way that programmers implement top-down design in their programs is by defining their own functions.  Often a programmer will write one sub-program or function for each subproblem.

A function is invoked (or called) when its name is encountered in the execution of a program.  When a function is called, the program control passes to the "called" function.  The calling function can pass information to the called function by passing arguments to the called function.  In the calling function the arguments are evaluated and the values are passed to the called function.

Every function has a header and a body.  A function header consists of:

The header is followed by the function body.

When the called function runs out of statements to execute, program control returns to the calling function.   The called function may return information to the invoking function.

Example

Consider the following example for computing the factorial of integers from 1 to 6.  It consists of a main program and a computational function that computes the factorial of an integer passed to it.

Option Explicit
Private Sub cmdFactorial_Click()
    Dim n As Integer, i As Integer, x As Integer
    For i = 1 To 6
         x = factorial(i)
         picOutput.Print " Factorial of "; i; " is "; x
    Next
End Sub
Private Function factorial(n As Integer) As Integer
    Dim i As Integer
    factorial  = 1
    For i = 2 To n
        factorial  = factorial  * i
    Next i
End Function

Assignment (Green and Ampt Infiltration)

(Chow, V., D. Maidment and L. Mays, Applied Hydrology, McGraw-Hill, 1988, pages 116 - 117)

Infiltration is the process of water penetrating from the ground surface into soil. Depending on the amount of infiltration and the soil properties, water may penetrate a few centimeters to several meters into a soil.  Cumulative infiltration is the total amount of water infiltrated during a given time period.  A commonly used infiltration equation is the Green-Ampt equation for the cumulative infiltration F after a period of time t 

(equation 1)

where:

F = cumulative infiltration (cm), (unknown)
K = soil conductivity (cm/hr), (use 0.65 cm/hr)
t = time of infiltration (hr), (use 1 hr)
y = suction head (cm), (use 16.7 cm)
Dq = difference in dry versus wet soil moisture, (use 0.34)
ln = natural logarithm

F, often used in hydraulic drainage design calculations, can be found by solving equation 1 for F.  One numerical method that can be used to solve this equation is fixed point iteration.  In fixed point iteration, we guess the value of F and substitute it into the right hand side of the equation

    (equation 2)

where Fi is the guessed value and Fi+1 is the new value.  Then we substitute this new value, F1, into the right-hand-side of the equations and obtain the next value F2

We continue this process until the values of F from one iteration to the next do not change very much (that is, they converge).  So, after i iterations, we have

    (equation 3)

The relative approximate error can be written as

When the relative approximate error is less than the required precision, we can stop our calculations.  That is, while

keep computing new approximations, when it falls below this value, stop.  Notice that the right hand side of equation 3 can be written as a function

Write a Visual Basic program that uses Fixed Point Iteration to solve for the infiltration F into the soil (Equation 3).  

Program: 

Choose a partner with whom you will develop and write your program.  Before leaving the laboratory, tell the TA the name of your partner.

In your program, use a function that evaluates g(Fi).  

Call this function from a loop to evaluate

The loop should continue until the relative approximate error is less than 1.0e-6.

Then the program should print out the final value of F.

Answer:  Oh, by the way, the answer is 3.17 cm of water infiltrated after one hour of infiltration.

Turn in:

Each team should create a word processor document containing the following information 

  1. Your VB source code.
  2. A screen shot of your program's result.

Email the word processor file as an email attachment to the TA.  Be sure to put the names of both team members at the top of the file.


McKinney | CE311K Civil Engineering | UT Austin