Chapter 5 : Method Flashcards

1
Q

What are method used to?

A
  1. 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
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the approach ( break a complex program into small, manageable pieces ) known as?

A
  1. Divide & Conquer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the general terms for breaking down a program to smaller units of code known as?

A
  1. Modularization
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

List out 2 parts for method definition

A
  1. Header
  2. body
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Where method header appears and indicate what?

A
  1. Appears at the beginning of a method
  2. Indicate access mode, return type, and method name
  • private void DisplayMessage() {}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between a void method and a normal method?

A
  1. void method does not return any value to the statement that called it
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a void method?

A
  1. A method that simply executes the statement it contains and then terminates
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the method body?

A
  1. Is a collection of statements that are performed when the method is executed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

List out the 4 parts of a method header

A
  1. Access Modifier
  2. Return Type
  3. Method Name
  4. Parameter ( Parentheses )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the access modifier? What value is available?

A
  1. Keywords that defines the access control
  2. Value Available
    • private
    • public
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between private and public method?

A
  1. 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 )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does return type specifies?

A
  1. Specifies whether or not a method returns a value
  • void - no return
    empty - return
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a method name?

A
  1. The identifier of the method, it must be unique in a given program
  • Like variable name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does a method’s name always followed by?

A
  1. A pair of parentheses ()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to create and display a method?

A

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.”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is return point?

A
  1. A memory address that the system saves to which it should return
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What will happen when the method ends?

A
  1. The system jumps back to the return point and resumes execution
18
Q

What does the system needs to know when calling a method?

A
  1. A system needs to know where the program should return after the method ends
19
Q

What techniques that the programmer commonly use to modularize a program?

A
  1. Top-down design
20
Q

What does a top-down design does?

A
  1. Breaks down an algorithm to methods
21
Q

List out the process for top-down design ( 3 )

A
  1. The overall task that the program is to perform is broken down into a series of subtasks
  2. Each subtask is examined to determine whether it can be further broken down into more subtasks
  3. Once all subtasks have been identified, they are written in code
  • Step 2 will be repeated until no more subtasks can be identified
22
Q

What is an argument?

A
  1. Any piece of data that is passed into a method when the method is called
  • MessageBox.Show(“Show”)
23
Q

What is a parameter?

A
  1. A variable that receives an argument that is passed into a method
  • private void DisplayValue (int value) {}
24
Q

What does string parameter accepts?

A
  1. Only string arguments
25
Q

What does int parameter accepts?

A
  1. Only int arguments
  2. Cannot pass double or decimal arguments
26
Q

What does double parameter accepts?

A
  1. double or int parameters
  2. Cannot pass decimal value to double parameters
27
Q

What does decimal parameter accepts?

A
  1. decimal , int parameters
  2. Cannot pass double value to decimal parameters
28
Q

Can you pass more than one argument to a method?

A
  1. Yes
29
Q

What is the syntax for named arguments?

A
  1. 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”);
}

30
Q

What does Default Argument does?

A
  1. Allow us to specify a default value when there isn’t arguements obtained by referencing the method
31
Q

How to write default arguments?

A

private void ShowTax(decimal price, decimal taxRate = 0.07m)
{
decimal tax = price * taxRate;
}

32
Q

What is a reference parameter?

A
  1. 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
33
Q

How to write code for reference parameter?

A

private void SetToZero(ref int number)
{
number =0;
}

int myVar = 99;
SetToZero(ref myVar);

34
Q

What is the difference between an output parameter and reference parameter? ( 2 )

A
  1. An argument doesn’t have to be a value before it is passed into an output parameter
  2. A method that has an output must be the output parameter to some value before it finishes executing
35
Q

How to write code for Output Parameters?

A

private void SetToZero(out int number)
{
number = 0;
}

SetToZero(out myVar);

36
Q

What is a value returning method?

A
  1. Is a method that returns a value to the part of the program that called it
37
Q

Why a value-returning method is like a void method ? ( 2 )

A
  1. It contains a group of statements that performs a specific task
  2. When I want to execute the method, I call it
38
Q

What statement will at the last of the statement of the method?

A
  1. return statement
39
Q

How to write a return statement inside a method?

A

private int sum(int num1, int num2)
{
return num1 + num2;
}

40
Q

How to write code to return boolean values?

A

private bool IsEven(int number)
{
bool numberIsEven;
if (number % 2 == 0)
{
numberIsEven = true;
}
else
{
numberIsEven = false;
}
return numberIsEven;
}