Methods

5. Methods#

When a calculation or text-cleaning step appears in several places, it needs a name and a clear promise. This chapter builds on the built-in calls from Chapter 2 to develop your own methods, parameters, results, and reusable string-processing operations. You will connect method design to caller behavior, scope, and repeatable checks rather than simply breaking a program into smaller files. A supporting reading at the end introduces recursive methods, in which a method calls itself on a smaller input; it is optional here and is recalled when trees arrive in Chapter 17.

  • Method definition: A method packages a named task so the program can call it when needed.

  • Method header and contract: The full declaration communicates call syntax; the contract states the required behavior. The C# signature is the narrower method identity used for overloading.

  • Parameters: Parameters let one method work with different input values.

  • Return values: A value-returning method computes a result that other code can store or combine.

Chapter Video

C# methods 📞 (6:04) by Bro Code.

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Define and call methods with names, parameter lists, and return types that communicate their responsibilities.

  2. Trace arguments, parameters, local scope, and returned values between a caller and a method.

  3. Distinguish a void method from a value-returning method and use a returned result in a larger expression.

  4. Decompose repeated calculations into methods with explicit input and result contracts.

  5. Clean, search, replace, and extract text with explicit string-matching policies while preserving the original string unless a result is assigned.

  6. Check methods with expected results and boundary cases, including reviewing a prepared AI suggestion against its contract.

Chapter Flow

Suggested three-meeting pace (110 minutes): read the relevant explanations and predict the selected example before class.

Meeting

Minutes

Guided focus

1

35

helper calls, arguments, and local state.

2

40

return values, contracts, and boundary checks.

3

35

string operations, composition, and refactoring.

Class uses selected traces, runs, and checks. Use the remaining worked examples and detailed API tables for practice and reference. Supporting readings are selected separately; lab and homework follow the core lessons.

Glossary

#

Term

Explanation

1

Method

A named block of code that performs a task and can be called from elsewhere.

2

Method call

An instruction that runs a method.

3

Parameter

A named input listed in a method definition.

4

Argument

A value supplied to a parameter during a method call.

5

Return type

The type of value a method sends back to its caller.

6

Return value

The actual value produced by a value-returning method.

7

Void method

A method that performs an action but does not return a value.

8

Signature

The method name together with its parameter list.

9

Scope

The region of code where a name can be used.

10

Local variable

A variable declared inside a method or block.

11

Helper method

A smaller method used to organize part of a larger task.

12

Refactoring

Changing code structure without changing its intended behavior.

13

Local scope

The region where a local name can be used.

14

Method contract

The required inputs, promised result, and stated failure behavior of a method.

15

String immutability

String operations produce results rather than changing the existing string object.

16

Boundary test

A check at or near an input-domain limit.

17

Recursion

A method calling itself on a smaller input until a base case answers directly.

18

Base case

The input a recursive method answers without calling itself.

19

Recursive case

The branch that calls the method on a smaller input and combines the result.

20

Call stack

The runtime’s record of unfinished method calls, each with its own parameters.

Lecture Slides

Chapter 5 lecture slides follow the current reading order. Supporting parts remain optional; instructor notes and editable decks are distributed separately.