Method.

Slides:



Advertisements
Similar presentations
Sub and Function Procedures
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
An Introduction to Programming with C++ Fifth Edition
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Example 2.
Sub Programs To Solve a Problem, First Make It Simpler.
Chapter 4 - Visual Basic Schneider
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 7: User-Defined Methods
Chapter 41 General Procedures Sub Procedures, Part I Sub Procedures, Part II Function Procedures.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
IE 212: Computational Methods for Industrial Engineering
Why to Create a Procedure
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
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.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Visual Basic CDA College Limassol Campus COM123 Visual Programming 1 Semester B Lecture:Pelekanou Olga Week 5: Useful Functions and Procedures.
Passing Parameters. 2 home back first prev next last What Will I Learn? List the types of parameter modes Create a procedure that passes parameters Identify.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Copyright © 2014 Pearson Education, Inc. Chapter 6 Procedures and Functions.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Subroutines and Functions Chapter 6. Introduction So far, all of the code you have written has been inside a single procedure. –Fine for small programs,
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
These materials where developed by Martin Schray
CS0004: Introduction to Programming
Sub Procedures And Functions
Chapter 9: Value-Returning Functions
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Royal University of Phnom Penh
Functions Chapter 6-Part 2.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
5.2 Sub Procedures, Part I Defining and Calling Sub Procedures
6 Chapter Functions.
Procedures and Functions
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Chapter 9: Value-Returning Functions
Methods.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
COMPUTER PROGRAMMING SKILLS
Standard Version of Starting Out with C++, 4th Edition
Corresponds with Chapter 5
Presentation transcript:

Method

Outline What is Procedures How to: Create a Procedure that Returns a Value How to: Return a Value from a Procedure How to: Call a Procedure That Returns a Value How to: Create and call a sub Procedure that don’t Returns a Value What is Procedure Overloading

procedures A procedure is a group of statements that together perform a task when called. After the procedure is executed, the control returns to the statement calling the procedure. Visual basic has two types of procedures: Functions Sub procedures or Subs Functions return a value, whereas Subs do not return a value. The term, method, describes a Sub or Function procedure that is accessed from outside its defining module, class, or structure.

Defining a Function A Function procedure is a series of Visual Basic statements enclosed by the Function and End Function statements. The Function procedure performs a task and then returns control to the calling code. When it returns control, it also returns a value to the calling code. Each time the procedure is called, its statements run, starting with the first executable statement after the Function statement and ending with the first End Function, Exit Function, or Return statement encountered. You can define a Function procedure in a module, class, or structure. It is Public by default, which means you can call it from anywhere in your application that has access to the module, class, or structure in which you defined it. A Function procedure can take arguments, such as constants, variables, or expressions, which are passed to it by the calling code.

Declaration Function Syntax The Function statement is used to declare the name, parameter and the body of a function. The syntax for the Function statement is: Where, Modifiers: specify the access level of the function; possible values are: Public, Private…etc FunctionName: indicates the name of the function ParameterList: specifies the list of the parameters ReturnType: specifies the data type of the variable the function returns [Modifiers] Function FunctionName [(ParameterList)] As ReturnType [Statements] End Function

Example Following code snippet shows a function FindMax that takes two integer values and returns the larger of the two. Function FindMax(ByVal num1 As Integer, ByVal num2 As Integer) As Integer Dim result As Integer If (num1 > num2) Then result = num1 Else result = num2 End If FindMax = result End Function

Function Returning a Value In VB.Net, a function can return a value to the calling code in two ways: By using the return statement By assigning the value to the function name Function fun1(ByVal num As Integer) As Double Dim result As Integer result = num*2 return num End Function Function fun1(ByVal num As Integer) As Double Dim result As Integer result = num*2 Fun1 = result End Function

Call function You invoke a Function procedure by including its name and arguments either on the right side of an assignment statement or in an expression. You must provide values for all arguments that are not optional, and you must enclose the argument list in parentheses. If no arguments are supplied, you can optionally omit the parentheses. The syntax for a call to a Function procedure is as follows: lvalue = functionname(argumentlist)  If ((functionname(argumentlist) / 3) <= expression) Then 

Calling example Module myfunctions Function FindMax(ByVal num1 As Integer, ByVal num2 As Integer) As Integer Dim result As Integer If (num1 > num2) Then result = num1 Else result = num2 End If FindMax = result End Function Sub Main() Dim a As Integer = 100 Dim b As Integer = 200 Dim res As Integer res = FindMax(a, b) Console.WriteLine("Max is : “+ res.tostring ) Console.ReadLine() End Sub End Module Output : Max is: 200

Defining a Sub Procedures A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End Sub statements. The Sub procedure performs a task and then returns control to the calling code, but it does not return a value to the calling code. Each time the procedure is called, its statements are executed, starting with the first executable statement after the Sub statement and ending with the first End Sub, Exit Sub By default, it is Public, which means you can call it from anywhere in your application that has access to the module, class, or structure in which you defined it. A Sub procedure can take arguments, such as constants, variables, or expressions, which are passed to it by the calling code.

Declaration Syntax The syntax for declaring a Sub procedure is as follows: The syntax for a call to a Sub procedure is as follows: [modifiers] Sub subname[(parameterlist)]  ' Statements of the Sub procedure.  End Sub  subname[(argumentlist)] 

Sub example Module Module1 Sub tellOperator(ByVal word As String) Console.WriteLine(word) End Sub Sub Main() Dim str As String = "ccsa227" tellOperator("hello") tellOperator(str) Console.ReadLine() End Module

Procedure Overloading Overloading a procedure means defining it in multiple versions, using the same name but different parameter lists. The purpose of overloading is to define several closely related versions of a procedure without having to differentiate them by name. You do this by varying the parameter list.

Overloading Rules When you overload a procedure, the following rules apply: Same Name. Each overloaded version must use the same procedure name. Different Signature. Each overloaded version must differ from all other overloaded versions in at least one of the following respects: Number of parameters Order of the parameters Data types of the parameters

Overloading Example output Sub cal(ByVal num1 As Integer, ByVal num2 As Integer) Dim result As Double result = num1 + num2 Console.WriteLine("Overloading 1: " + result.ToString) End Sub output Overloading 1: 6 Overloading 3: 8.5 Overloading 2: 14 Sub cal(ByVal num1 As Integer, ByVal num2 As Integer, ByVal num3 As Integer) Dim result As Double result = num1 + num2 Console.WriteLine("Overloading 2: " + result.ToString) End Sub Sub cal(ByVal num1 As Integer, ByVal num2 As Double) Dim result As Double result = num1 + num2 Console.WriteLine("Overloading 3: " + result.ToString) End Sub Sub Main() cal(2, 4) cal(3, 5.5) cal(4, 5, 5) Console.ReadLine() End Sub