Chapter6 - Chapter 6 Procedures 1 Introduction Modular...

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

View Full Document Right Arrow Icon
1 Chapter 6 Procedures
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Introduction Modular Programming Procedures Functions Subroutines Sample Programs PermandComb.vb BubbleSort.vb
Background image of page 2
3 Modular Programming Partitioning the code into pieces called modules Each module (procedure) implements a relatively small but well-defined task. Results in a hierarchical structure
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Modular Programming Why ? Code will be shorter and easier to follow The program becomes less susceptible to errors Debugging becomes easier Modification becomes easier Promotes code-reusability
Background image of page 4
5 Procedures in VB.NET A procedure is a block of code with a specific purpose and a given name. Subroutines A sub does not have a return value. Example: event-handler sub Functions A function has a return value: it passes a value back to the location from where it was called.
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Procedures We have used Subs (event-handlers) before: -Key_Down -Btn_Click -TextBox_TextChanged We have used functions before too: -CInt(n) -IsNumeric (value) -FormatNumber (x,z)
Background image of page 6
7 Functions We focus on functions first. (subroutines are similar) Functions typically compute something and return a result. (Subroutines have no explicit output) Our first function example is on the next page…
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Sample Program: Simple Function Write a program that computes and displays the value of the math function shown below using a function procedure. - - < < - = 3 ) 3 ( 3 1 2 3 1 ) ( 2 x x x x x x x f
Background image of page 8
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 PermAndComb Objective: Write a program that computes the permutation and combination based on two numbers entered in text boxes. New Topics: Function Procedures Link label control Launching default web-browser from visual studio
Background image of page 10
11 Sample Runs Link Label
Background image of page 11

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

View Full DocumentRight Arrow Icon
12 Sample Runs
Background image of page 12
13 PermandComb.vb Permutation Formula Combination Formula We will write our own factorial function. )! ( ! r n n - ! )! ( ! r r n n -
Background image of page 13

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

View Full DocumentRight Arrow Icon
14 Program Hierarchy btnGo_Click Combination Permutation Factorial Level 1 Level 2 Level 3
Background image of page 14
15 Factorial Function Factorial() computes and returns the factorial of a specified value. The Return statement terminates the function and returns the current value of fact back to the caller procedure. 3 6
Background image of page 15

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

View Full DocumentRight Arrow Icon
16 Function Header Function Name Parameter List Return Type Keywords Private Function factorial ( ByVal k As Long ) As Long Return type specifies what kind of output the function returns. Parameter list contains the inputs passed to the function. Do not worry about Private and ByVal.
Background image of page 16
Background image of page 17

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

View Full DocumentRight Arrow Icon
Background image of page 18
Background image of page 19

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

View Full DocumentRight Arrow Icon
20 Invoking (Calling) a Function A programmer-developed function is invoked (called) exactly the same as one of the above Perm = permutation (n, r) Comb = combination (n, r) Str =FormatNumber( x , 2 ) Value = CDbl (y) Int = CInt (z) Bool = IsNumeric (value) Visual Basic provides
Background image of page 20
Multiple Return Statements Even the Long type cannot handle more than 20!. If the input is greater than 20, our function
Background image of page 21

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

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

This note was uploaded on 10/25/2011 for the course COP 2271 taught by Professor Staff during the Fall '11 term at University of Florida.

Page1 / 76

Chapter6 - Chapter 6 Procedures 1 Introduction Modular...

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

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