Methods Session 04 Mata kuliah: M0874 – Programming II Tahun: 2010.

Slides:



Advertisements
Similar presentations
Pemrograman VisualMinggu …6… Page 1 MINGGU Ke Enam Pemrograman Visual Pokok Bahasan: Module, Class & Methods Tujuan Instruksional Khusus: Mahasiswa dapat.
Advertisements

C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
Arrays Session 05 Mata kuliah: M0874 – Programming II Tahun: 2010.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
 2006 Pearson Education, Inc. All rights reserved Functions.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Introduction to Computers and Programming Introduction to Methods in Java.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
History  We first begin with Java which was released in 1995 by Sun Microsystems  Initially Java was 100% interpreted at runtime and was very slow 
 2007 Pearson Education, Inc. All rights reserved C Functions.
1 MATERI PENDUKUNG METHOD Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
1 C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
1 Console methods Write and WriteLine also have the capability to display formatted data. Figure 3.17 shows another way to use the WriteLine method. Outline.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
4-Methods Dr. John P. Abraham Professor UTPA. Common ways of packaging code Properties Methods Classes Namespaces (related classes are grouped into a.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
1 Recursion Recursive method –Calls itself (directly or indirectly) through another method –Method knows how to solve only a base case –Method divides.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
 Prentice Hall. All rights reserved. 1 Recursion (Section 7.13 & Exercise7.40 from ed.3) (Sections 6.15, 6.16 from ed.1) Many slides modified.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C++ Lecture 2 Friday 11 July Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
BIM313 – Advanced Programming File Operations 1. Contents Structure of a File Reading/Writing Texts from/to Files File and Directory Operations 2.
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
CSC 298 Streams and files.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math -Class Methods 6.4.
Introduction to C# Anders Hejlsberg Distinguished Engineer Developer Division Microsoft Corporation.
C++ Programming Lecture 12 Functions – Part IV
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
Functions Course conducted by: Md.Raihan ul Masood
RECURSION.
C++.
Programming Fundamentals Lecture #7 Functions
Deitel- C:How to Program (5ed)
Computing with C# and the .NET Framework
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 6 - Functions Outline 5.1 Introduction
Module 2 Variables, Assignment, and Data Types
Programming Fundamentals Lecture #7 Functions
Presentation transcript:

Methods Session 04 Mata kuliah: M0874 – Programming II Tahun: 2010

Bina Nusantara University 3 Outline Materi Program Modules in C# Methods Definitions Value Types and Reference Types Passing Arguments: Pass-by-Value vs. Pass-by- Reference Recursion Recursion vs. Iteration

Bina Nusantara University 4 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 ( ) );

Bina Nusantara University 5 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 6 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 7

8

9

10

Bina Nusantara University 11 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 12 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.

13 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.

Bina Nusantara University 14 Pass-by-Value

Bina Nusantara University 15 Pass-by-Reference

Bina Nusantara University 16 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

17 Recursion example namespace Recursion { class IterateFolders { public static void Main(string[] args) { DirectoryInfo dir = new 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();

18 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.

Bina Nusantara University 19 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 20 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 21 References sharpcorner.com/UploadFile/myoussef/CSharpMeth odsP_ AM/CSharpMethodsP_1.a spxhttp:// sharpcorner.com/UploadFile/myoussef/CSharpMeth odsP_ AM/CSharpMethodsP_1.a spx 4.htmlhttp:// 4.html p.aspxhttp:// p.aspx