19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

3. In Appendix D, the section “Stepping Through a Program Containing a Procedure:<br />

Section 3” uses the <strong>Visual</strong> <strong>Basic</strong> debugger to trace the flow through a program<br />

<strong>and</strong> observe the interplay between arguments <strong>and</strong> parameters.<br />

4. You can use the Print method in a Form_Load event procedure. If so, you must<br />

set the AutoRedraw property of the picture box to True. Otherwise the contents<br />

of the picture box will be erased when the event procedure terminates.<br />

3.3 FUNCTION PROCEDURES<br />

<strong>Visual</strong> <strong>Basic</strong> has many built-in functions. In one respect, functions are like miniature programs.<br />

They use input, they process the input, <strong>and</strong> they have output. Some functions we encountered<br />

earlier are listed in Table 3.1.<br />

TABLE 3.1<br />

Some <strong>Visual</strong> <strong>Basic</strong> Built-In Functions<br />

Function Example Input Output<br />

Int Int(2.6) is 2 number number<br />

Chr Chr(65) is “A” number string<br />

Len Len(“perhaps”) is 7 string number<br />

Mid Mid(“perhaps”,4,2) is “ha” string, string<br />

number,<br />

number<br />

InStr InStr(“to be”,“ ”) is 3 string,string number<br />

Although the input can involve several values, the output always consists of a single<br />

value. The items inside the parentheses can be constants (as in Table 3.1), variables, or<br />

expressions.<br />

In addition to using built-in functions, we can define functions of our own. These new<br />

functions, called Function procedures or user-defined functions, are defined in much the<br />

same way as Sub procedures <strong>and</strong> are used in the same way as built-in functions. Like builtin<br />

functions, Function procedures have a single output that can be string or numeric. Function<br />

procedures can be used in expressions in exactly the same way as built-in functions.<br />

Programs refer to them as if they were constants, variables, or expressions. Function procedures<br />

are defined by function blocks of the form<br />

Private Function FunctionName(var1 As Type1, var2 As Type2, ...) As dataType<br />

statement(s)<br />

FunctionName = expression<br />

End Function<br />

The variables in the top line are called parameters, <strong>and</strong> variables inside the function block that<br />

are not parameters have local scope. Function names should be suggestive of the role performed<br />

<strong>and</strong> must conform to the rules for naming variables. The type dataType, which specifies<br />

the type of the output, will be one of String, Integer, Single, <strong>and</strong> so on. In the preceding<br />

general code, the next-to-last line assigns the output, which must be of type dataType, to the<br />

function name. Two examples of Function procedures are as follows:<br />

Private Function FtoC(t As Single) As Single<br />

‘Convert Fahrenheit temperature to Celsius<br />

FtoC = (5 / 9) * (t - 32)<br />

End Function<br />

Private Function FirstName(nom As String) As String<br />

Dim firstSpace As Integer<br />

‘Extract the first name from the full name nom<br />

firstSpace = InStr(nom, “ “)<br />

Function Procedures 87

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!