1 st Semester 2005 1 Module 6 C# Methods – Part II อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Objects contains data and methods Class – type of object Create class first Then object or instance String – defined class String s; // creates instance.
Method parameter passing, file input/output Computer and Programming.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Method Type of Method No Returned value Returned value Parameter Passing No Parameter Pass by value Pass.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
STRUCT Thanachat Thanomkulabut 1. Array Review 2  Group multiple items of the same type into one "variable" double[] score; score = new.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Method Parameters and Overloading Version 1.0. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
METHOD 2 Thanachat Thanomkulabut 1. 2 Outline Type of Method No Returned value Returned value No Parameter Pass by value refout Pass by reference Parameter.
Department of Computer Engineering Methods Computer Programming for International Engineers.
Final Review Author: Thanachat Thanomkulabut Edited by Supaporn Erjongmanee Final Review 22 September 2011.
INPUT/OUTPUT STATEMENT ADDITION SLIDES. Console.ReadLine() – Use to get the input (String) from user Convert string to other data type – int.Parse() 
C# Programming Methods.
1 nd Semester Module2 C# Basic Concept Thanawin Rakthanmanon Computer Engineering Department Kasetsart University, Bangkok.
1 st Semester Module11 Struct Type in C# Thanawin Rakthanmanon Create by: Aphirak Jansang Computer Engineering Department.
FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types.
1 st Semester Module3 Condition Statement อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Lecture 2 Review of 1301 and C# Richard Gesick. Common data types int, long, double, float, char, bool, string.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Parameter passing Jordi Cortadella Department of Computer Science.
1 st Semester Module4-1 Iteration statement - while อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Examples of Classes & Objects
AKA the birth, life, and death of variables.
COMP 170 – Introduction to Object Oriented Programming
Parameter Passing & Array Examples – Part 1
Chapter 4 Procedural Methods.
More Object Oriented Programming
Method Mark and Lyubo.
Lecture 11 C Parameters Richard Gesick.
Pass by Reference, const, readonly, struct
An Introduction to Java – Part II
Classes & Objects: Examples
Java Lesson 36 Mr. Kalmes.
Pass by Reference.
CS2011 Introduction to Programming I Methods (II)
Module5 Looping Techniques: Part 2
Module5 Looping Techniques: Part 2
Chapter 6 Methods.
AKA the birth, life, and death of variables.
Chapter 7 Procedural Methods.
METHODS, CLASSES, AND OBJECTS A FIRST LOOK
Module8 Multi-dimensional Array
Module 8 & 9 Method :Part I & Array :Part II
Thanachat Thanomkulabut
Class.
The Lifecycle of an Object
Functions and Recursion
Unit-1 Introduction to Java
Presentation transcript:

1 st Semester Module 6 C# Methods – Part II อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department Kasetsart University, Bangkok THAILAND

1 st Semester /21 Outline  C# Method Review  Parameter Passing Pass by reference Pass by value

1 st Semester /21 C# Structure – Multiple MethodsNamespace Class Main() Method1() Method2() Method3() MethodNN()

1 st Semester /21 Method Types Method Type PredefinedMethodUser-definedMethod Returnvalue No return valueReturnvalue value

1 st Semester /21 Method Types Method Type PredefinedMethodUser-definedMethod Returnvalue No return valueReturnvalue value

1 st Semester /21 Method Declaration static ( ) { ; } #remark return-type can be - data type = int, double, string, … need return statement need return statement - void = return no value

1 st Semester /21 Method Example: with return value … int x = power2(2); … static int power2(int n) { int j = n * n; return j; int j = n * n; return j;} *Methods can return only single value.

1 st Semester /21 Method Example: without return value … DisplayName(”Aphirak”); … static void DisplayName(string s){ Console.WriteLine( ” Hello, {0}”,s); Console.WriteLine( ” Hello, {0}”,s);}

1 st Semester /21 Method Example

1 st Semester /21 Outline  C# Method Review  Passing Parameter Pass by value Pass by reference

1 st Semester /21 Passing Parameter Example 1 … int x = power2(2); … static int power2(int n) { int j = n * n; return j; int j = n * n; return j;}

1 st Semester /21 Passing Parameter Example 2 … int x = power2(2); … static int power2(int n) { n = n * n; return n; n = n * n; return n;}

1 st Semester /21 Passing Parameter Type Passing Type Pass by value Pass by reference

1 st Semester /21 Outline  C# Method Review  Passing Parameter Pass by value Pass by reference

1 st Semester /21 Pass by value Example … string s; s = ”InitialD”; DisplayMovie(s); Console.WriteLine(s); … static void DisplayMovie(string st) { Console.WriteLine(”Movie = {0}”,st) Console.WriteLine(”Movie = {0}”,st) st = ”TomYumKung”; st = ”TomYumKung”;} Copy value s to st

1 st Semester /21 Outline  C# Method Review  Passing Parameter Pass by value Pass by reference

1 st Semester /21 Pass by reference Example … string s; s = ”InitialD”; DisplayMovie(ref s); Console.WriteLine(s); … static void DisplayMovie( ref string st) { Console.WriteLine(”Movie = {0}”,st) Console.WriteLine(”Movie = {0}”,st) st = ”TomYumKung”; st = ”TomYumKung”;} s is referred by st Changes st/changes s too

1 st Semester /21 How to write method: swap x and y x = 5; y = 4; swap( x, y); Console.WriteLine(”x={0}, y={1}”,x,y); swap(ref x, ref y);

1 st Semester /21 Keywords ref and out  C# also provides another pass by reference keyword: out  ref & out: static void Add(int a, int b, out int c) { c = a + b; } refout un-initialized variables are not allowed any variables are allowed previous values are passed to methods no previous values are passed to methods

1 st Semester /21 Example  Min(x,y)  Find min value  Max(x, y)  Find max value  Mul(x, y)  Find x * y  IsPrime(x)  Is x a prime number?  CircleArea(r)  Find area or circle with radius r  BahtToDollar(b)  Convert baht to dollar

1 st Semester /21 Summary  Method Return Value: int, double, etc… void  Passing Parameter Pass by value Pass by reference