Professional Documents
Culture Documents
• What Is a Method?
• Declaring and Invoking Methods
– Void and Return type Methods
• Methods with Parameters
• Overloading Methods
• Program Execution Flow
• Naming and Best Practices
2
WHAT IS A METHOD
Void Method
Simple Methods
8
Invoking a Method
14
Solution: Sign of Integer Number
15
Problem: Grades
16
Optional Parameters
18
Solution: Printing Triangle
19
Solution: Printing Triangle (2)
• Create a method that prints the first half (1..n) and then
the second half (n-1…1) of the triangle:
Method with
static void PrintTriangle(int n) parameter n
{
for (int line = 1; line <= n; line++)
PrintLine(1, line); Lines 1...n
20
RETURNING VALUES FROM METHODS
The Return Statement
22
Using the Return Values
23
Problem: Calculate Rectangle Area
5 50 7
10 56
8
24
Solution: Calculate Rectangle Area
String StringString
2
26
Problem: Math Power
27
OVERLOADING METHODS
Method Signature
30
Signature and Return Type
string
aaa bbb
bbb
32
PROGRAM EXECUTION FLOW
Program Execution
43
NAMING AND BEST PRACTICES
Naming Methods
38
Naming Method Parameters
39
Methods – Best Practices