22 Functions and Procedures

22 Functions and Procedures - Functions and Subroutines CEE...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Functions and Subroutines CEE 3804 Computer Applications for CEE Modular Programming circle6 A consequence of top-down design is that the problem is decomposed into smaller and simpler sub-problems circle6 The program is broken up into a number of smaller subprograms or modules circle6 This approach of designing programs as a series of modules is called modular design circle6 A module is a small self-contained section of an algorithm circle6 Modular design has a number of advantages Advantages of Modular Design circle6 Modular programs are easier to write and debug circle6 Each module can be written and tested independently circle6 Modular programs are easier to debug circle6 Modules can be changes, rewritten or even replaced circle6 Previously developed and tested modules can be used in different programs circle6 Can develop a library of modules Procedures in Visual Basic circle6 There are two types of procedures (subprograms) in Visual Basic circle6 Subroutines Sub .. .. END Sub Can return zero or more values. Cannot be used in as expression circle6 Functions Function .. .. End Function Can return only one value. Can be used in expressions. Similar to the Visual Basic built-in functions such as Abs, Log, Sin Functions circle6 A function consists of a block of instructions that begins with the Function statement and ends with an End Function circle6 Functions are invoked the same way as Visual Basic built-in functions circle6 You specify the name of the function in expressions, as arguments in statements or other functions circle6 Can use a function in any place you can use a built-in function circle6 Functions can only return one value Functions circle6 Syntax Function functionName (parmlist) .. .. .. .. .. .. functionName = .. .. .. End Function assign value to functionName Functions circle6 Parameter list contains variables that will receive values when the function is called circle6 These variables are also called formal parameters circle6 Must have an assignment statement in the function body that assigns a value to functionName circle6 The value assigned to functionName is the value returned from the function Functions circle6 General Format [Private/Public] Function funcName [ parmlist ] [As type] funcName = expression End Function funcName is the name of the function parmlist is the list of formal parameters type specifies the type of value expression is any general expression that has the same data type as funcName Function Names circle6 The return type indicates the type of value returned by the function Function MaxValue(.. ..) as Integer returns an integer value Function SomeString(.. ..) as String returns a string Function ALargeNum(.. ..) as Double returns a double-precision value Functions circle6 The name of the function establishes a data type for the value returned by the function circle6 The same rules used to designate the data types of a variable apply to user-defined functions Function Square (x as Single) As Single...
View Full Document

Page1 / 37

22 Functions and Procedures - Functions and Subroutines CEE...

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

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