Document 15063152

advertisement
Mata kuliah : M0874 – Programming II
Tahun
: 2010
Methods
Session 04
Outline Materi
•Program Modules in C#
•Methods Definitions
•Value Types and Reference Types
•Passing Arguments: Pass-by-Value vs. Pass-byReference
•Recursion
•Recursion vs. Iteration
Bina Nusantara University
3
Program Modules in C#
•Modules in C# are called methods and classes.
•Programmer can write methods to define specific
tasks that may be used at many points in a program.
•For example math class methods, allow the
programmer to perform certain common
mathematical calculations.
•To calculate and print the square root of 900.0:
Console.WriteLine( Math.Sqrt ( 900.0 ) );
Bina Nusantara University
4
Methods Definitions
•Programs contained at least one method definition
(such as Main).
•Several motivations for modularizing a program
with methods:
•Divide-and-conquer approach makes program
development more manageable.
•Software reusability-using existing methods (and classes)
as building blocks to create new program.
•To avoid repeating code in a program.
Bina Nusantara University
5
Methods Definitions
•With proper method naming and definition, we can
create programs from standardized methods, rather
than building customized code.
•For example, we did not have to define how to
convert strings to integers-The .Net Framework
Class Library already defines such methods for us.
Bina Nusantara University
6
Bina Nusantara University
7
Bina Nusantara University
8
Bina Nusantara University
9
Bina Nusantara University
10
Value Types and Reference Types
•C# includes built-in value types and reference
types.
•Value types normally represent single pieces of
data, such as int or bool values.
•The built-in value types are the integral types (sbyte,
byte, char, short, ushort, int, uint, long, and ulong),
the floating-point types (float and double) and the
types decimal and bool.
Bina Nusantara University
11
Value Types and Reference Types
•Reference types, on the other hand, refer to
objects, which can contain many individual pieces of
data.
•The built-in reference types are string and object.
•Programmers also can create value types and
reference types. The reference types that
programmers can create are classes, interfaces and
delegates.
Bina Nusantara University
12
Passing Arguments: Pass-by-Value vs. Pass-by-Reference
•When an argument is passed by value, the called method receives a copy
of the argument’s value.
•When an argument is passed using pass-by-reference, the caller gives the
method the ability to access and modify the caller’s original data directly.
•Pass-by-reference can improve performance because it eliminates the
overhead of copying large data items such as objects;
•However, pass-by-reference can weaken security because the called
methods can modify the caller’s data.
13
Pass-by-Value
Bina Nusantara University
14
Pass-by-Reference
Bina Nusantara University
15
Recursion
•A recursive method is a method that calls itself
either directly or indirectly through another method.
•For some problems, it is useful to have a method
actually cal itself.
•Advantages of recursion:
•Game programming
•Artificial Intelligence
•Most of the mathematic functions can be presented in the form of
recursion
•For some problems, it very easy to understand them using recursion
Bina Nusantara University
16
Recursion example
namespace Recursion {
class IterateFolders {
public static void Main(string[] args) {
DirectoryInfo dir = new DirectoryInfo(@"C:\Program Files\Adobe\Acrobat 5.0");
getDirsFiles(dir);
}
public static void getDirsFiles(DirectoryInfo d){
FileInfo [] files;
files = d.GetFiles("*.*");
foreach (FileInfo file in files) {
String fileName = file.FullName;
String fileSize = file.Length.ToString();
String fileExtension =file.Extension;
String fileCreated = file.LastWriteTime.ToString();
17
Recursion example
io.WriteLine(fileName + " " + fileSize + " " + fileExtension + " " + fileCreated);
}
DirectoryInfo [] dirs = d.GetDirectories("*.*")
foreach (DirectoryInfo dir in dirs) {
io.WriteLine("--------->> {0} ", dir.Name);
getDirsFiles(dir);
}
}
}
}
The output will show all folders for the specified folder and list the details of each file
including full path, size, extension and date created.
18
Recursion vs. Iteration
•Both iteration and recursion are based on a control structure.
•Iteration uses a repetition structure (such as for, while, do/while),
recursion uses a selection structure (such as if, if/else or switch)
•Both iteration and recursion involve repetition.
•Iteration explicitly uses a repetition structure and recursion achieves
repetition through repeated method calls.
•Iteration and recursion each involve a termination test.
•Iteration terminates when the loop-continuation condition fails and
recursion terminates when a base case is recognized.
Bina Nusantara University
19
Recursion vs. Iteration
•Any problem that can be solved recursively also
can be solved iteratively (nonrecursively).
•Recursive solutions also are chosen when iterative
solutions are not apparent.
•Avoid using recursion in performance situation.
Recursive calls take time and consume additional
memory.
Bina Nusantara University
20
References
•http://www.csharpcorner.com/UploadFile/myoussef/CSharpMeth
odsP_111152005003025AM/CSharpMethodsP_1.a
spx
•http://www.brpreiss.com/books/opus6/html/page59
4.html
•http://www.codeproject.com/KB/cs/recursionincshar
p.aspx
Bina Nusantara University
21
Download