Chapter 5 : Method Flashcards
What are method used to?
- Break a complex program into small, manageable pieces
public partial class Form1 : Form
{
private void myButton_Click(object sender, EventArgs e)
{
Method2();
}
private void Method2()
{
Statement
}
What is the approach ( break a complex program into small, manageable pieces ) known as?
- Divide & Conquer
What is the general terms for breaking down a program to smaller units of code known as?
- Modularization
List out 2 parts for method definition
- Header
- body
Where method header appears and indicate what?
- Appears at the beginning of a method
- Indicate access mode, return type, and method name
- private void DisplayMessage() {}
What is the difference between a void method and a normal method?
- void method does not return any value to the statement that called it
What is a void method?
- A method that simply executes the statement it contains and then terminates
What is the method body?
- Is a collection of statements that are performed when the method is executed
List out the 4 parts of a method header
- Access Modifier
- Return Type
- Method Name
- Parameter ( Parentheses )
What is the access modifier? What value is available?
- Keywords that defines the access control
- Value Available
- private
- public
What is the difference between private and public method?
- Access control ( private method can be called only by code inside the same class while public class can be called by code outside the class )
What does return type specifies?
- Specifies whether or not a method returns a value
- void - no return
empty - return
What is a method name?
- The identifier of the method, it must be unique in a given program
- Like variable name
What does a method’s name always followed by?
- A pair of parentheses ()
How to create and display a method?
private void goButton_Click(object sender, EventArgs e)
{
MessageBox.Show(“This is the goButton_Click method.”);
DisplayMessage();
}
private void DisplayMessage()
{
MessageBox.Show(“This is the DisplayMessage method.”);
}
What is return point?
- A memory address that the system saves to which it should return
What will happen when the method ends?
- The system jumps back to the return point and resumes execution
What does the system needs to know when calling a method?
- A system needs to know where the program should return after the method ends
What techniques that the programmer commonly use to modularize a program?
- Top-down design
What does a top-down design does?
- Breaks down an algorithm to methods
List out the process for top-down design ( 3 )
- The overall task that the program is to perform is broken down into a series of subtasks
- Each subtask is examined to determine whether it can be further broken down into more subtasks
- Once all subtasks have been identified, they are written in code
- Step 2 will be repeated until no more subtasks can be identified
What is an argument?
- Any piece of data that is passed into a method when the method is called
- MessageBox.Show(“Show”)
What is a parameter?
- A variable that receives an argument that is passed into a method
- private void DisplayValue (int value) {}
What does string parameter accepts?
- Only string arguments
What does int parameter accepts?
- Only int arguments
- Cannot pass double or decimal arguments
What does double parameter accepts?
- double or int parameters
- Cannot pass decimal value to double parameters
What does decimal parameter accepts?
- decimal , int parameters
- Cannot pass double value to decimal parameters
Can you pass more than one argument to a method?
- Yes
What is the syntax for named arguments?
- parameterName : value
- Named Argument allows us to specify which prameter an argument should be passed into
private void showButton_Click(object sender, EventArgs e)
{
showName(lastName : “Smith”, firstName : “Suzanne”);
}
What does Default Argument does?
- Allow us to specify a default value when there isn’t arguements obtained by referencing the method
How to write default arguments?
private void ShowTax(decimal price, decimal taxRate = 0.07m)
{
decimal tax = price * taxRate;
}
What is a reference parameter?
- A special type of parameter tht does not receive a copy of the argument’s value
- It becomes a reference to the argument that was passed into it
How to write code for reference parameter?
private void SetToZero(ref int number)
{
number =0;
}
int myVar = 99;
SetToZero(ref myVar);
What is the difference between an output parameter and reference parameter? ( 2 )
- An argument doesn’t have to be a value before it is passed into an output parameter
- A method that has an output must be the output parameter to some value before it finishes executing
How to write code for Output Parameters?
private void SetToZero(out int number)
{
number = 0;
}
SetToZero(out myVar);
What is a value returning method?
- Is a method that returns a value to the part of the program that called it
Why a value-returning method is like a void method ? ( 2 )
- It contains a group of statements that performs a specific task
- When I want to execute the method, I call it
What statement will at the last of the statement of the method?
- return statement
How to write a return statement inside a method?
private int sum(int num1, int num2)
{
return num1 + num2;
}
How to write code to return boolean values?
private bool IsEven(int number)
{
bool numberIsEven;
if (number % 2 == 0)
{
numberIsEven = true;
}
else
{
numberIsEven = false;
}
return numberIsEven;
}