Chapter 6: Procedures and Functions
6.1 Procedures
- A procedure (Sub procedure) is a named block of code that performs a task and does NOT return a value
- Declared with Sub ... End Sub
- When called, app branches to the procedure, executes it, then resumes at the next statement after the call
- General-purpose procedures are NOT tied to a specific control/event
- The Call keyword is optional in VB
6.2 Passing Arguments to Procedures
- Arguments — values passed to a procedure/function when called
- Parameter — special variable that receives an argument
- If a parameter is declared, you MUST supply an argument when calling
- Left-to-right assignment: arguments match parameters in order
- Pass by value (ByVal) — a COPY is passed; procedure cannot modify original
- Pass by reference (ByRef) — procedure has access to original and CAN modify it
- ByVal is the default if no keyword is specified
6.3 Functions
- A function is a named block of code that performs a task and RETURNS a value
- Declared with Function ... End Function
- Specify return type: Function Name(args) As DataType
- Return a value with the Return statement
- Functions can return non-numeric values (String, Boolean, etc.)
- Call a function by using it in an expression or assigning to a variable
6.4 Debugging: Step Into, Over, Out
- Step Into (F11) — single-step through each line, including called procedures/functions
- Step Over (F10) — execute a procedure/function without stepping through its code
- Step Out (Shift+F11) — execute remaining statements in current procedure and return to caller
- Use Step Over when you trust a procedure and don't need to see its internals
- Use Step Out to fast-exit a procedure you accidentally stepped into
Key Concepts
- Modularize — dividing an application's code into small, manageable procedures/functions
- Local variables are created when procedure starts, destroyed when it ends
- Static local variables retain their value between procedure calls
- Pascal casing — naming convention where first letter of each word is capitalized
- A method is a procedure or function that belongs to an object/class
💡 Key Tip: Know the difference between ByVal vs ByRef, procedure vs function, and the 3 debugging commands (Step Into/Over/Out). These are the most tested concepts!
📝 Practice Quiz — Chapter 6
10 questions. Select your answer, then check the result.
🃏 Flashcards — Chapter 6
15 cards. Click the card to flip. Use buttons to navigate.
Press a button to start
Card 1 of 15
📋 Cheat Sheet — Chapter 6
🔑 Procedure vs Function
PROCEDURE (Sub): FUNCTION:
Sub Name(args) Function Name(args) As DataType
' code ' code
End Sub Return value
End Function
- Procedure: NO return value
- Function: RETURNS a value via Return🔑 Passing Arguments
ByVal → Passes a COPY (default)
ByRef → Passes a REFERENCE (can modify original)
Sub Calc(ByVal intA As Integer, ByRef intB As Integer)
intA = 10 ' Only changes local copy
intB = 20 ' Changes original variable!
End Sub🔑 Procedure Declaration & Call
' Declaration
Sub DisplayMsg(ByVal strMsg As String)
MessageBox.Show(strMsg)
End Sub
' Call (Call keyword is optional)
DisplayMsg("Hello")
Call DisplayMsg("Hello")🔑 Debugging Commands
Step Into (F11) → Single-step into called procedures Step Over (F10) → Execute procedure without stepping in Step Out (F11+F11) → Finish current procedure, return to caller
🔑 Key Vocabulary
Argument → Value passed when calling Parameter → Variable that receives the argument Static → Preserves local variable between calls Modularize → Break code into small procedures PascalCase → Naming: EachWordCapitalized
Tip: Trace through code examples manually — write down what each argument's value is and whether it's ByVal or ByRef.