{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

22 Functions and Procedures

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

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

View Full Document Right Arrow Icon
Functions and Subroutines CEE 3804 Computer Applications for CEE
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
Functions circle6 Syntax Function functionName (parmlist) .. .. .. .. .. .. functionName = .. .. .. End Function assign value to functionName
Image of page 6
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
Image of page 7

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

View Full Document Right Arrow Icon
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
Image of page 8
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
Image of page 9

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

View Full Document Right Arrow Icon
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 Square = x * x End Function circle6 Function Square can be called as follows: y = Square(x) y = Square(10) z = y * Square(x)
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern