Introduction to Methods ISYS 350. Methods Methods can be used to break a complex program into small, manageable pieces – This approach is known as divide.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Starting Out with Java: From Control Structures through Objects Fourth.
Introduction to C++ Functions Chapter 6 Sections 6.1 – 6.3 Computer II.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
1 Fall 2009ACS-1903 Methods – Ch 5 A design technique referred to as stepwise refinement (or divide and conquer, or functional decomposition) is used to.
An Introduction to Programming with C++ Fifth Edition
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 6: User-Defined Functions I
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modularizing Your Code with Methods.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
C# Introduction ISYS 512. Major Differences Between VB Project and C# Project The execution starts from the Main method which is found in the Program.cs.
Introduction to Methods
Chapter 6: Functions.
Apply Sub Procedures/Methods and User Defined Functions
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design.
Decision Structure - 1 ISYS 350. Decision: Action based on condition Examples Simple condition: – If total sales exceeds $300 then applies 5% discount;
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
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.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C# Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the properties.
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Methods CSCI 1301 What is a method? A method is a collection of statements that performs a specific task. Pre-Defined methods: available in Java library.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Chapter 5 : Methods. Why Write Methods?  Methods are commonly used to break a problem down into small manageable pieces. This is called divide and conquer.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
Decision Structure - 1 ISYS 350. Decision: Action based on condition Examples Simple condition: – If total sales exceeds $300 then applies 5% discount;
Programming Fundamentals Enumerations and Functions.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
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.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
Chapter 5 Methods. 2 Contents 1. Introduction to Methods 2. Passing Arguments to a Method 3. More about Local Variables 4. Returning a Value from a Method.
Sub Procedures And Functions
by Tony Gaddis and Godfrey Muganda
Object Oriented Systems Lecture 03 Method
CSCI 161: Introduction to Programming Function
Decision Structure - 1 ISYS 350.
Chapter Topics Chapter 5 discusses the following main topics:
Chapter 3 Introduction to Classes, Objects Methods and Strings
Starting Out with Java: From Control Structures through Objects
Chapter 4 void Functions
6 Chapter Functions.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 6 – Methods Topics are:
Starting Out with Java: From Control Structures through Objects
Lecture 5- Classes, Objects and Methods
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
ITM 352 Functions.
Presentation transcript:

Introduction to Methods ISYS 350

Methods Methods can be used to break a complex program into small, manageable pieces – This approach is known as divide and conquer – In general terms, breaking down a program to smaller units of code, such as methods, is known as modularization Two types of methods are: – A void method simply executes a group of statements and then terminates without returning a value Sub program – A value-returning method returns a value to the statement that called it Function

void Methods A void method simply executes the statement it contains and then terminates. It does not return any value to the statement that called it To create a method you write its definitions A method definition has two parts: – header: the method header appears at the beginning of a method definition to indicate access mode, return type, and method name – body: the method body is a collection of statements that are performed when the method is executed

Void Method The book separates a method header into four parts : – Access modifier: keywords that defines the access control private: a private method can be called only by code inside the same class as the method public: a public method can be called by code that is outside the class. – Return type: specifies whether or not a method returns a value – Method name: the identifier of the method; must be unique in a given program. – Parentheses: A method’s name is always followed by a pair of parentheses private void DisplayMessage() { MessageBox.Show(“This is the DisplayMessage method.”); } Access modifier Return type Method name Parentheses

Value-Returning Methods (Functions) In C# the generic format is: AccessModifier DataType MethodName(ParameterList) { statement(s); return expression; } AccessModifier: private or public DataType: int, double, decimal, string, and Boolean MethodName: the identifier of the method; must be unique in a program ParameterList: an optional list of parameter Expression: can be any value, variable, or expression that has a value A return statement is used to return a value to the statement that called the method.

MyPayment Method private double myPayment(double loan,double rate,double term) { double payment; payment = (loan * rate / 12) / (1 - Math.Pow(1 + rate / 12, -12 * term)); return payment; }

Using MyPayment Method private void button1_Click(object sender, EventArgs e) { double loan, rate, term; loan = double.Parse(textBox1.Text); rate = double.Parse(textBox2.Text); term = double.Parse(textBox3.Text); textBox4.Text = myPayment(loan, rate, term).ToString("c"); }

MyIsNumeric Function private Boolean myIsNumeric(string inputString) { try { double.Parse(inputString); return true; } catch { return false; }

Using myIsnumeric for Validation private void textBox1_Validating(object sender, CancelEventArgs e) { if (!myIsNumeric(textBox1.Text)) { MessageBox.Show("Enter digit only"); e.Cancel=true; }

Passing Arguments to Methods By Value Example: private void DisplayValue(int value) { MessageBox.Show(value.ToString()); } int x = 5; DisplayValue(x); DisplayValue(x * 4);

Passing Arguments by Reference A reference parameter is a special type of parameter that does not receive a copy of the argument’s value It becomes a reference to the argument that was passed into it When an argument is passed by reference to a method, the method can change the value of the argument in the calling part of the program In C#, you declare a reference parameter by writing the ref keyword before the parameter variable’s data type private void SetToZero(ref int number) { number =0; } To call a method that has a reference parameter, you also use the keyword ref before the argument int myVar = 99; SetToZero(ref myVar); What is the value of myVar after calling the SetToZero method?

Input/Output Arguments private void myDouble(ref double AnyNumber) { AnyNumber = 2 * AnyNumber; } private double myDouble2(double input, ref double output) { output = 2 * input; return output; } private void myDouble3(double input, ref double output1,ref double output2) { output1 = 2 * input; output2 = 3 * input; }