SlideShare a Scribd company logo
1 of 34
Visual Basic Chapter 6 - Procedures
Sub Procedures Sub Procedure - A set of statements that perform a specific task. Divide a program into smaller, more manageable blocks of code. An event procedure is written for a specific object event, while a sub procedure can perform tasks unrelated to any specific event. Form:Sub ProcedureName()     statementsEnd Sub
Sub Procedures ProcedureName - A name describing the task performed by the procedure. Should indicate an action Should begin with an uppercase letter and then an uppercase letter should begin each word within the name  (Like standard variable declarations.) May not contain spaces Sub procedures divide a program into a smaller, more manageable blocks of code.
Sub Procedures Sub Procedures result in less code redundancy Statements for a specific task appear just once in the Sub procedure. Example on page 163 A Sub procedure must be called with a Call statement from another procedure in order to execute. ,[object Object],[object Object]
Changing an Image at Run Time Images must be stored in the Resources folder in the project folder. The My.Resources object is used to access an image Form:My.pictureBox.Image = My.Resources.imageNamepictureBox- name of the PictureBox objectimageName- name of the resource in the Resources folder
The LinkLabel Control The LinkLabel Control is used to add a link to a website. (Name) should begin withlnk. ActiveLinkColor sets the color of the link when clicked. LinkColor sets the color of the link. Text sets the text for the destination website. VisitedLinkColor sets the color of a previously visited link.
Review: PetStorepage 165
Value Parameters A procedure often needs data in order to complete its task. pass - giving data to a procedure argument - a variable or value passed to a procedure Call GiveHint(secretNumber, guess) arguments: secretNumber, guess parameter - a variable declared in a procedure to accept the value or address of an argument
Value Parameters Parameters are used inside the procedure to perform the procedure's task. Form:Sub ProcedureName(ByValparameter1 As type, ...) statements End Sub ByVal- keyword used to declare a value parameter in a procedure or function.
Value Parameters value parameter -a variable declaration in a procedure to accept a copy of an argument. cannot alter the value of the actual arguments used in the procedure call. GiveHint() example - page 167 Remember when working with procedures that have value parameters: The order of the arguments passed corresponds to the order of the parameters in the procedure heading.
Value Parameters The number of arguments in a procedure call must match the number of parameters in the procedure declaration. Arguments passed by value can be in the form of constants, variables, values, or expressions. Variable arguments passed by value are not changed by the procedure. Example: Demo() - page 168
Procedure Documentation Just as comments are used to clarify statements, comments should also be used to describe, or document, procedures. Procedure documentation should include a brief description of what the procedure does. Documentation should also include preconditions and postconditions. precondition - The initial requirements of a procedure (e.g. assumptions).  Also referred to as “pre”.
Procedure Documentation postcondition - A statement of what must be true at the end of the execution of a procedure if the procedure has worked properly.  Also referred to as “post”. Note: A procedure may not have a precondition, but every procedure must have a postcondition. Example: GiveHint() - pages 168-169
Review: GuessingGame - part 3 of 4page 169
Reference Parameters reference parameter - A variable declared in a procedure to accept to address of an argument. Can alter the value of the variable arguments used in the procedure call Form: Sub ProcedureName(ByRefparameter1 As type, ...) statements End Sub
Reference Parameters ByRef - Keyword used to declare a reference parameter in a procedure. Note: A procedure can have both reference (ByRef) and value (ByVal) parameters.Example:TwoDigits()- page 170 address - A variable’s location in memory where its value is stored. ,[object Object],[object Object]
Reference Parameters Keep in mind when working with procedures with reference parameters: The order of the arguments passed corresponds to the order of the parameters in the procedure heading. ByRef parameters accept only variable arguments. Variable arguments passed by reference may be changed by the procedure.
Review: NumberBreakdownpage 171Review: SortNumberspage 173
Control Object Parameters Control objects, such as labels, are a data type called a control class. Includes CheckBox, Label, RadioButton, Button, TextBox and PictureBox controls. A control object can be passed as an argument to a procedure. Control argument parameters should be declared  ByRef in a procedure using the appropriate class name. Example: GiveHint() - page 174
Review: GuessingGame - part 4 of 4page 174
Event Handler Procedures Event procedures are used in every program. Called event handler procedures because they execute in response to an event. Can be written to handle more than one event. In the following example, the event procedure executes when the check box is clicked:
Event Handler Procedures Handles - Keyword that determines which events cause an event procedure to execute. Event procedure declarations include the Handles keyword followed by the events to be handled. The keyword, not the procedure name, determines when an event procedure executes. Therefore, changing an event procedure name has no effect on procedure execution. Event procedures handle multiple events when event names, separated by commas, are added after the Handles keyword.
Event Handler Procedures Event procedures always include two parameters: An object to raise the event Information about the event  chkRelish_Click example: ,[object Object]
e is the information about the eventObject - A data type that can be used to represent any value.
The HotDog Application
The Tag Property Every control has a Tag property. tag - Control class property that can be set to a string expression for identifying objects. When an event procedure is handling more than one object, the string in the Tag property can be used to determine which object raised the event. Important when different actions should be taken for different objects. Example: Page 176
Review: ShellGamepage 176
Function Procedures function - A set of statements that perform a specific task and then return a value. Built-in functions include Int() and Rnd(). Form:Function ProcedureName(ByValparameter1 As _ type, ...) As Returntypestatements   Return valueEnd Function Return - Statement used in a function procedure to send a value back to the calling statement.
Function Procedures Function Procedures are called from within an assignment statement or another type of statement that will make use of the returned value. Example:Me.lblStudentGrade.Text = LetterGrade(average) See page 180. Note: A function is a better choice over a Sub procedure when a well-defined task that results in a single value is to be performed.
Function Procedures Keep in mind the following when working with functions: The order of the arguments corresponds to the order of the parameters. Only ByVal parameters should be declared in a function because a function should not alter the arguments it has been passed. A function returns a single value and therefore must be used in a statement such as an assignment statement that makes use of the returned value.
Review: LetterGradepage 181
Coding Conventions Procedures names should indicate an action and begin with an uppercase letter and then an uppercase letter should begin each word within the name. Procedure documentation should include a brief description of the task the procedure performs, a precondition when needed, and a postcondition.
Case Study: CalculatorIIpage 182Review: CalculatorII 1 of 2page 189Review: CalculatorII 2 of 2page 189Review: TestRndIntFunctionpage 189
Chapter 6 slides

More Related Content

What's hot

Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
Deepak Singh
 
Qtp Presentation
Qtp PresentationQtp Presentation
Qtp Presentation
techgajanan
 
Booa8 Slide 09
Booa8 Slide 09Booa8 Slide 09
Booa8 Slide 09
oswchavez
 
VB Function and procedure
VB Function and procedureVB Function and procedure
VB Function and procedure
pragya ratan
 

What's hot (20)

Functions assignment
Functions assignmentFunctions assignment
Functions assignment
 
Presentation of computer
Presentation of computerPresentation of computer
Presentation of computer
 
4. function
4. function4. function
4. function
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
 
Function & Recursion
Function & RecursionFunction & Recursion
Function & Recursion
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
Function
FunctionFunction
Function
 
Processes in Query Optimization in (ABMS) Advanced Database Management Systems
Processes in Query Optimization in (ABMS) Advanced Database Management Systems Processes in Query Optimization in (ABMS) Advanced Database Management Systems
Processes in Query Optimization in (ABMS) Advanced Database Management Systems
 
Qtp Presentation
Qtp PresentationQtp Presentation
Qtp Presentation
 
Booa8 Slide 09
Booa8 Slide 09Booa8 Slide 09
Booa8 Slide 09
 
Python recursion
Python recursionPython recursion
Python recursion
 
Maximum Price Limitation
Maximum Price LimitationMaximum Price Limitation
Maximum Price Limitation
 
Procedures functions structures in VB.Net
Procedures  functions  structures in VB.NetProcedures  functions  structures in VB.Net
Procedures functions structures in VB.Net
 
Basics of cpp
Basics of cppBasics of cpp
Basics of cpp
 
Understanding Subroutines and Functions in VB6
Understanding Subroutines and Functions in VB6Understanding Subroutines and Functions in VB6
Understanding Subroutines and Functions in VB6
 
VB Function and procedure
VB Function and procedureVB Function and procedure
VB Function and procedure
 
Class 10
Class 10Class 10
Class 10
 
Python - Lecture 2
Python - Lecture 2Python - Lecture 2
Python - Lecture 2
 

Similar to Chapter 6 slides

Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 

Similar to Chapter 6 slides (20)

Function & procedure
Function & procedureFunction & procedure
Function & procedure
 
Unit iii vb_study_materials
Unit iii vb_study_materialsUnit iii vb_study_materials
Unit iii vb_study_materials
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
Functions and Arguments in Python
Functions and Arguments in PythonFunctions and Arguments in Python
Functions and Arguments in Python
 
procedures and arrays
procedures and arraysprocedures and arrays
procedures and arrays
 
Day2 j meter_training_script_enhancements
Day2 j meter_training_script_enhancementsDay2 j meter_training_script_enhancements
Day2 j meter_training_script_enhancements
 
Day2_Apache_JMeter_Script_Enhancements
Day2_Apache_JMeter_Script_EnhancementsDay2_Apache_JMeter_Script_Enhancements
Day2_Apache_JMeter_Script_Enhancements
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
Cpp functions
Cpp functionsCpp functions
Cpp functions
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
Savitch ch 04
Savitch ch 04Savitch ch 04
Savitch ch 04
 
Python-Functions.pptx
Python-Functions.pptxPython-Functions.pptx
Python-Functions.pptx
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
 
Unit iv functions
Unit  iv functionsUnit  iv functions
Unit iv functions
 
Functions in c language
Functions in c languageFunctions in c language
Functions in c language
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Visualbasic tutorial
Visualbasic tutorialVisualbasic tutorial
Visualbasic tutorial
 
Subprogramms
SubprogrammsSubprogramms
Subprogramms
 
Ch4 functions
Ch4 functionsCh4 functions
Ch4 functions
 

Recently uploaded

會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
中 央社
 

Recently uploaded (20)

TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
Open Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPointOpen Educational Resources Primer PowerPoint
Open Educational Resources Primer PowerPoint
 
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...Navigating the Misinformation Minefield: The Role of Higher Education in the ...
Navigating the Misinformation Minefield: The Role of Higher Education in the ...
 
Essential Safety precautions during monsoon season
Essential Safety precautions during monsoon seasonEssential Safety precautions during monsoon season
Essential Safety precautions during monsoon season
 
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptxREPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 
Behavioral-sciences-dr-mowadat rana (1).pdf
Behavioral-sciences-dr-mowadat rana (1).pdfBehavioral-sciences-dr-mowadat rana (1).pdf
Behavioral-sciences-dr-mowadat rana (1).pdf
 
“O BEIJO” EM ARTE .
“O BEIJO” EM ARTE                       .“O BEIJO” EM ARTE                       .
“O BEIJO” EM ARTE .
 
factors influencing drug absorption-final-2.pptx
factors influencing drug absorption-final-2.pptxfactors influencing drug absorption-final-2.pptx
factors influencing drug absorption-final-2.pptx
 
....................Muslim-Law notes.pdf
....................Muslim-Law notes.pdf....................Muslim-Law notes.pdf
....................Muslim-Law notes.pdf
 
Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17
 
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 

Chapter 6 slides

  • 1. Visual Basic Chapter 6 - Procedures
  • 2. Sub Procedures Sub Procedure - A set of statements that perform a specific task. Divide a program into smaller, more manageable blocks of code. An event procedure is written for a specific object event, while a sub procedure can perform tasks unrelated to any specific event. Form:Sub ProcedureName() statementsEnd Sub
  • 3. Sub Procedures ProcedureName - A name describing the task performed by the procedure. Should indicate an action Should begin with an uppercase letter and then an uppercase letter should begin each word within the name (Like standard variable declarations.) May not contain spaces Sub procedures divide a program into a smaller, more manageable blocks of code.
  • 4.
  • 5. Changing an Image at Run Time Images must be stored in the Resources folder in the project folder. The My.Resources object is used to access an image Form:My.pictureBox.Image = My.Resources.imageNamepictureBox- name of the PictureBox objectimageName- name of the resource in the Resources folder
  • 6. The LinkLabel Control The LinkLabel Control is used to add a link to a website. (Name) should begin withlnk. ActiveLinkColor sets the color of the link when clicked. LinkColor sets the color of the link. Text sets the text for the destination website. VisitedLinkColor sets the color of a previously visited link.
  • 8. Value Parameters A procedure often needs data in order to complete its task. pass - giving data to a procedure argument - a variable or value passed to a procedure Call GiveHint(secretNumber, guess) arguments: secretNumber, guess parameter - a variable declared in a procedure to accept the value or address of an argument
  • 9. Value Parameters Parameters are used inside the procedure to perform the procedure's task. Form:Sub ProcedureName(ByValparameter1 As type, ...) statements End Sub ByVal- keyword used to declare a value parameter in a procedure or function.
  • 10. Value Parameters value parameter -a variable declaration in a procedure to accept a copy of an argument. cannot alter the value of the actual arguments used in the procedure call. GiveHint() example - page 167 Remember when working with procedures that have value parameters: The order of the arguments passed corresponds to the order of the parameters in the procedure heading.
  • 11. Value Parameters The number of arguments in a procedure call must match the number of parameters in the procedure declaration. Arguments passed by value can be in the form of constants, variables, values, or expressions. Variable arguments passed by value are not changed by the procedure. Example: Demo() - page 168
  • 12. Procedure Documentation Just as comments are used to clarify statements, comments should also be used to describe, or document, procedures. Procedure documentation should include a brief description of what the procedure does. Documentation should also include preconditions and postconditions. precondition - The initial requirements of a procedure (e.g. assumptions). Also referred to as “pre”.
  • 13. Procedure Documentation postcondition - A statement of what must be true at the end of the execution of a procedure if the procedure has worked properly. Also referred to as “post”. Note: A procedure may not have a precondition, but every procedure must have a postcondition. Example: GiveHint() - pages 168-169
  • 14. Review: GuessingGame - part 3 of 4page 169
  • 15. Reference Parameters reference parameter - A variable declared in a procedure to accept to address of an argument. Can alter the value of the variable arguments used in the procedure call Form: Sub ProcedureName(ByRefparameter1 As type, ...) statements End Sub
  • 16.
  • 17. Reference Parameters Keep in mind when working with procedures with reference parameters: The order of the arguments passed corresponds to the order of the parameters in the procedure heading. ByRef parameters accept only variable arguments. Variable arguments passed by reference may be changed by the procedure.
  • 19. Control Object Parameters Control objects, such as labels, are a data type called a control class. Includes CheckBox, Label, RadioButton, Button, TextBox and PictureBox controls. A control object can be passed as an argument to a procedure. Control argument parameters should be declared ByRef in a procedure using the appropriate class name. Example: GiveHint() - page 174
  • 20. Review: GuessingGame - part 4 of 4page 174
  • 21. Event Handler Procedures Event procedures are used in every program. Called event handler procedures because they execute in response to an event. Can be written to handle more than one event. In the following example, the event procedure executes when the check box is clicked:
  • 22. Event Handler Procedures Handles - Keyword that determines which events cause an event procedure to execute. Event procedure declarations include the Handles keyword followed by the events to be handled. The keyword, not the procedure name, determines when an event procedure executes. Therefore, changing an event procedure name has no effect on procedure execution. Event procedures handle multiple events when event names, separated by commas, are added after the Handles keyword.
  • 23.
  • 24. e is the information about the eventObject - A data type that can be used to represent any value.
  • 26. The Tag Property Every control has a Tag property. tag - Control class property that can be set to a string expression for identifying objects. When an event procedure is handling more than one object, the string in the Tag property can be used to determine which object raised the event. Important when different actions should be taken for different objects. Example: Page 176
  • 28. Function Procedures function - A set of statements that perform a specific task and then return a value. Built-in functions include Int() and Rnd(). Form:Function ProcedureName(ByValparameter1 As _ type, ...) As Returntypestatements Return valueEnd Function Return - Statement used in a function procedure to send a value back to the calling statement.
  • 29. Function Procedures Function Procedures are called from within an assignment statement or another type of statement that will make use of the returned value. Example:Me.lblStudentGrade.Text = LetterGrade(average) See page 180. Note: A function is a better choice over a Sub procedure when a well-defined task that results in a single value is to be performed.
  • 30. Function Procedures Keep in mind the following when working with functions: The order of the arguments corresponds to the order of the parameters. Only ByVal parameters should be declared in a function because a function should not alter the arguments it has been passed. A function returns a single value and therefore must be used in a statement such as an assignment statement that makes use of the returned value.
  • 32. Coding Conventions Procedures names should indicate an action and begin with an uppercase letter and then an uppercase letter should begin each word within the name. Procedure documentation should include a brief description of the task the procedure performs, a precondition when needed, and a postcondition.
  • 33. Case Study: CalculatorIIpage 182Review: CalculatorII 1 of 2page 189Review: CalculatorII 2 of 2page 189Review: TestRndIntFunctionpage 189