April 05, 2022
Nikolay Kostov (Nikolay.IT)
Topics covered:
- Using Methods
- What is a Method? Why to Use Methods?
- Declaring and Creating Methods
- Calling Methods
- Methods with Parameters
- Passing Parameters
- Returning Values
- Best Practices
Video (in Bulgarian)
Presentation Content
What is a Method?
- A method is a kind of building block that solves a small problem
- A piece of code that has a name and can be called from the other code
- Can take parameters and return a value
- Methods allow programmers to construct large programs from simple pieces
- Methods are also known as functions , procedures , and subroutines
Why to Use Methods?
- More manageable programming
- Split large problems into small pieces
- Better organization of the program
- Improve code readability
- Improve code understandability
- Avoiding repeating code
- Improve code maintainability
- Code reusability
- Using existing methods several times
Declaring and Creating Methods
- Each method has a name
- It is used to call the method
- Describes its purpose
- Methods declared static can be called by any other method (static or not)
- This will be discussed later in details
- The keyword void means that the method does not return any result
Calling Methods
- To call a method, simply use:
- The method’s name
- Parentheses (don’t forget them!)
- A semicolon ( ; )
- This will execute the code in the method’s body and will result in printing the following:
- A method can be called from:
- The Main() method
- Any other method
- Itself (process known as recursion )
Method Parameters
- To pass information to a method, you can use _parameters _ (also known as arguments )
- You can pass zero or several input values
- You can pass values of different types
- Each parameter has name and type
- Parameters are assigned to particular values when the method is called
- Parameters can change the method behavior depending on the passed values
Calling Methods with Parameters
- To call a method and pass values to its parameters:
- Use the method’s name, followed by a list of expressions for each parameter
- Expressions must be of the same type as method’s parameters (or compatible)
- If the method requires a float expression, you can pass int instead
- Use the same order like in method declaration
- For methods with no parameters do not forget the parentheses
Returning Values From Methods
- A method can return a value to its caller
- Returned value:
- Can be assigned to a variable
- Can be used in expressions
- Can be passed to another method
Defining Methods That Return a Value
- Instead of void , specify the type of data to return
- Methods can return any type of data ( int , string , array, etc.)
- void methods do not return anything
- The combination of method’s name and parameters is called method signature
- Use return keyword to return a result
The return Statement
- The return statement:
- Immediately terminates method’s execution
- Returns specified expression to the caller
- To terminate void method, use just:
- Return can be used several times in a method body
Methods – Best Practices
- Each method should perform a single, well-defined task
- Method’s name should describe that task in a clear and non-ambiguous way
- Good examples: CalculatePrice , ReadName
- Bad examples: f , g1 , Process
- In C# methods should start with capital letter
- Avoid methods longer than one screen
- Split them to several shorter methods
Summary
- Break large programs into simple methods that solve small sub-problems
- Methods consist of declaration and body
- Methods are invoked by their name
- Methods can accept parameters
- Parameters take actual values when calling a method
- Methods can return a value or nothing