Professional Documents
Culture Documents
Topic 6 - Methods
Outline
6.1
6.2
6.3
6.4
6.5
6.6
6.7
6.8
6.9
6.10
6.11
6.12
6.13
6.14
6.15
6.16
6.17
Introduction
Program Modules in C#
Math Class Methods
Methods
Method Definitions
Argument Promotion
C# Namespaces
Value Types and Reference Types
Passing Arguments: Call-by-Value vs. Call-by-Reference
Random-Number Generation
Example: Game of Chance
Duration of Identifiers
Scope Rules
Recursion
Example Using Recursion: The Fibonacci Series
Recursion vs. Iteration
Method Overloading
worker1
worker4
Fig. 6.1
worker2
worker3
worker5
Constants
Math.PI = 3.1415926535
Math.E = 2.7182818285
Description
Example
Ceiling( x )
Cos( x )
trigonometric cosine of x
(x in radians)
Exp( x )
exponential method ex
Floor( x )
Log( x )
Log( 2.7182818284590451 )
is approximately 1.0
Log( 7.3890560989306504 )
is approximately 2.0
Max( x, y )
Min( x, y )
Pow( x, y )
Sin( x )
trigonometric sine of x
(x in radians)
Sqrt( x )
square root of x
Tan( x )
trigonometric tangent of x
(x in radians)
absolute value of x
Abs( x )
Fig. 6.2
Commonly used
6.4 Methods
Variables
Declared in a method = local variables
Declared outside a method = global variables
Only the method that defines them know they exist
Send parameters to communicate with other methods
Body
Contains the code of what the method does
Contains the return value if necessary
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Outline
System;
// includes basic data types
System.Drawing;
// for graphics capabilities
System.Collections; Start
// for
complex
data structures
of class
SquareInteger.
It implements
System.ComponentModel; // controls component behavior
System.Windows.Forms; System.Windows.Forms.Form
// for GUI development
System.Data;
// for reading outside data
Subtract.cs
method
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// loop 10 times
for ( int counter = 1; counter <= 10; counter++ )
{
// calculate square of counter and store in result
result = Square( counter );
The main body
of the
SquareIntegers method
Outline
Subtract.cs
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Outline
Subtract.cs
Program Output
10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Outline
MaximumValue.cs
class MaximumValue
{
// main entry point for application
static void Main( string[] args )
{
// obtain user input and convert to double
Console.Write( "Enter first floating-point value: " );
double number1 = Double.Parse( Console.ReadLine() );
11
28
29
30
31
32
33
34
35
36
37
Outline
MaximumValue.cs
Program Output
12
13
Explicit Conversion
Object is manually converted
Required if there could be a loss of data
Widening
Make an object that of a derived class and more complex
Narrowing
Make an object that of a base class and cause some data loss
14
byte
decimal, double, float, int, uint, long, ulong, object, short or ushort
sbyte
char
decimal
object
double
object
float
double or object
int
uint
long
ulong
object
None
short
ushort
string
object
bool
Fig. 6.5
object
15
6.7 C# Namespaces
Namespace
16
6.7 C# Namespaces
Namespace
Description
System.Data
System.Drawing
System.IO
Contains classes for the input and output of data, such as with
files.
System.Threading
System.Windows.Forms
System.Xml
System
Fig. 6.6
17
Reference types
Contain an address to a spot in memory where the data is
Programmer create
Classes (Chapter 8)
Interfaces (Chapter 8)
Delegates (Chapter 9)
Passing by reference
Send a method the actual reference point
Causes the variable to be changed throughout the program
18
19
randomObject.Next()
Returns a number from 0 to Int32.MaxValue
Int32.MaxValue = 2,147,483,647
randomObject.Next( x )
Returns a value from 0 up to but not including x
randomObject.Next( x, y )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Outline
RandomInt.cs
// loop 20 times
for ( int i = 1; i <= 20; i++ )
{
// pick random integer between 1 and 6
value = randomInteger.Next( 1, 7 );
output += value + " "; // append value to output
// if counter divisible by 5, append newline
if ( i % 5 == 0 )
output += "\n";
} // end for structure
20
31
32
33
34
35
36
Outline
RandomInt.cs
Program Output
21
22
Scope
The section of a program in which the object can be
referenced
Local variables
Created when declared
Destroyed when the block exits
Not initialized
23
Block scope
24
6.14 Recursion
Recursive methods
Methods that call themselves
Directly
Indirectly
Call others methods which call it
25
6.14 Recursion
Final value = 120
5!
5!
5! = 5 * 24 = 120 is returned
5 * 4!
4! = 4 * 6 = 24 is returned
4 * 3!
3! = 3 * 2 = 6 is returned
3 * 2!
5 * 4!
4 * 3!
3 * 2!
2 * 1!
1
(a) Procession of recursive calls.
2! = 2 * 1 = 2 is returned
2 * 1!
1
1 returned
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Outline
System;
System.Drawing;
System.Collections;
System.ComponentModel;
System.Windows.Forms;
System.Data;
FactorialTest.cs
26
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Outline
FactorialTest.cs
else
return number * Factorial( number - 1 );
}
Program Output
27
F(0) = 0
F(1) = 1
F(n) = F(n - 1) + F(n - 2)
Recursion is used to evaluate F(n)
Complexity theory
28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Outline
System;
System.Drawing;
System.Collections;
System.ComponentModel;
System.Windows.Forms;
System.Data;
FibonacciTest.cs
29
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Outline
FibonacciTest.cs
30
Outline
FibonacciTest.cs
Program Output
31
F( 3 )
return F( 2 )
return F( 1 )
return 1
F( 0 )
F( 1 )
return 1
return 0
32
33
Recursion
Uses selection structures
if, if/else, switch
34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Outline
System;
System.Drawing;
System.Collections;
System.ComponentModel;
System.Windows.Forms;
System.Data;
MethodOverload.cs
35
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Outline
MethodOverload.cs
[STAThread]
static void Main()
{
Application.Run( new MethodOverload() );
}
} // end of class MethodOverload
Program Output
36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Outline
MethodOverload2.cs
using System;
class MethodOverload2
{
public int Square( double x )
This
{
return x * x;
}
37
a double number
Program Output