Lec 09 Methods 1
Lec 09 Methods 1
What is a Method?
Basic Method Structure
Components of a Method
Method Return Types
Method Overloading
Call a Method
Passing Arguments by value and by reference
Optional Parameters
Named Parameters
Delegates
Recursion
2
What is a Method?
Basic Structure:
returnType MethodName(parameters)
{
// Method body
}
3
Motivation of Using Methods
Without Methods (Repetitive Code) With Methods (Using Methods to Avoid Repetition)
Reusability: The method CalculateArea can be reused for any rectangle without rewriting the same logic.
Maintainability: If the formula for calculating the area changes, you only need to update the method, not every
occurrence in the code.
Clarity: CalculateArea clearly tells you what the program is doing without needing to focus on the details of
how the calculation is performed. This is the power of abstraction in programming: it improves clarity by
separating the what from the how.
Avoids Redundancy: No need to repeat the same code, making the program cleaner and easier to maintain.
5
Components of a Method
Return Type: Type of data the method returns (e.g., void, int, string).
Method Name: Unique identifier for the method.
Parameters: Optional. Data passed into the method.
Method Body: Contains the code executed when the method is called.
Example:
6
Method Return Types
7
Method Overloading
A method's signature consists of the method's name and the number and type of its parameters.
Method Overloading is a concept where multiple methods have the same name but different parameters.
The return type is not considered part of the method signature.
Example of Overloading:
1. void PrintMessage(string message) {
2. Console.WriteLine(message);
3. }
Overloaded methods are chosen at compile-time based on the number or types of arguments passed to them.
9
Invalid Overloading based only on Return Type
If you try to overload a method by only changing its return type, the C# compiler will throw an error because the
method signatures are not unique:
If overloading considered return
1. static int Calculate() type, the compiler couldn't decide
2. { which method to call, since the
return type alone doesn't clarify
3. return 42;
the intended action. Method
4. } overloading should be based on
5. // Invalid: Cannot overload based on return type alone parameters, which define the
6. static string Calculate() method's behavior.
7. {
8. return "Hello";
9. }
Compile-error: already defines a member called 'Calculate' with the same parameter types
10
Calling a Method
int result = Add(5, 7); // Calling Add method and storing result
// If you don't need to store or use the return value, you can still call the method.
However, the result will be computed and then discarded after execution.
Add(5, 7);
11
Method Parameters
Definition: Parameters are values passed to a method to use within its body.
Types of Parameters:
o Value Parameters: Method parameters that get a copy of the argument value.
o Reference Parameters: Parameters passed by reference, using the ref or out keyword.
o Optional Parameters: Parameters with default values.
o Named Parameters: Passing arguments by explicitly specifying the parameter name, often in any order.
12
Passing Arguments By Value
Value Parameters: A copy of the argument is passed, and changes inside the method do not affect the original value.
Initialization:
o ref: The variable passed to the method must be initialized before the method is called.
o out: The variable passed to the method does not need to be initialized before the method is called.
Method Behavior:
o ref: The method can modify the passed argument, but the argument must already have a value when passed.
o out: The method must assign a value to the argument before the method completes, and the argument can be treated as
uninitialized before the call.
16
Passing an Array by Value (Default)
When array is passed by value to a method, the method receives a reference to the original array. However, this does not mean that the
array itself is passed by reference. Any changes made to the elements of the array will affect the original array, but reassigning the array
itself (i.e., pointing it to a new array) will not affect the original array.
// Output: 4, 5, 6 18
Thank you