Functions + Overloading + Scope

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
Chapter 5: METHODS USER-DEFINED METHODS. user-defined  We learned that a Java application program is a collection of classes, and that a class is a collection.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 7 User-Defined Methods.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
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.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Classes - Intermediate
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
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.
Chapter 7: User-Defined Methods J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Third Edition Third.
Chapter 9: Value-Returning Functions
User-Written 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
Chapter 1.2 Introduction to C++ Programming
Methods Chapter 6.
Object Oriented Systems Lecture 03 Method
Methods.
User-Defined Functions
Chapter 4 Procedural Methods.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
CSC1201: Programming Language 2
Classes, Objects, and Methods
Chapter 6 Methods: A Deeper Look
Object Oriented Programming (OOP) LAB # 5
6 Chapter Functions.
Review for Final Exam.
Group Status Project Status.
CHAPTER 6 GENERAL-PURPOSE METHODS
Chapter 7 Procedural Methods.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Chapter 7: User-Defined Functions II
Chapter 6: User-Defined Functions I
Review for Final Exam.
In C Programming Language
CSC1201: Programming Language 2
CSC1201: Programming Language 2
Defining Classes and Methods
Classes, Objects and Methods
CS1201: Programming Language 2
Lecture 8 Object Oriented Programming (OOP)
CPS125.
Corresponds with Chapter 5
Presentation transcript:

Functions + Overloading + Scope Chapter 10

Function A Function : is a group of statements that together perform a task. It can be used anywhere in the program. Why we need function? – Organize code in program – Code are easier to maintain For large programs, it is not practical to put the entire programming instructions into one method, You must learn to break the problem into manageable pieces A program can contain one or many functions Must always have a function called “main”. The main function is the starting point of all programs The compiler will not compile the code unless it finds a function called “main” within the program.

Function Method declaration is composed of: Method header. Method body When we need function? – When you need to repeat the same process over and over in a program. – The function can be called many times but appears in the code once (The function are reusable.) functions may be called Procedures or Routines and in object oriented programming called Methods . Method declaration is composed of: Method header. Method body <method header> { <method body> }

Predefined functions Predefined functions are functions that are built into java Language to perform some standard operations. The java standard library provides numerous built-in functions that your program can call. For example, function substring() . the definitions have been written and it is ready to be used.

User defined functions Function that been created by the user. – This functions need to be declared and defined by the user

Syntax modifiers: public, private, protected, static, abstract, final returnType: type of the value that the method calculates and returns (using return statement) methodName: Java identifier; name of method formal parameter list (arguments) : The syntax of the formal parameter list is:

functions Value returning functions: Void functions: functions that have a return type. These functions return a value of a specific data type using the return statement. Void functions: functions that do not have a return type. These functions do not use a return statement to return a value.

EXAMPLE calculate rectangle area Method Definition public static void Rectanglearea ( ) { System.out.println(“Enter the dimensions of your rectangle ”); int L=read.nextInt(); int W=read.nextInt(); System.out.println( “ the area is “ + (W*L) ) ; } Method Call Rectanglearea ( );

EXAMPLE calculate rectangle area Method Definition public static int Rectanglearea ( ) { System.out.println(“Enter the dimensions of your rectangle ”); int L=read.nextInt(); int W=read.nextInt(); int area = W*L ; return area ; } Method Call int x = Rectanglearea ( );

EXAMPLE calculate rectangle area Method Definition public static int Rectanglearea (int L , int W ) { Return W*L ; } Method Call Int length = 7 , width = 6 ; int x = Rectanglearea ( length , width );

Exercise Write a Function larger, which returns the larger of the two given integers. Write a Function Square, which returns the square of the given integer.

Example :Largest Number Input: set of 5 numbers stored in array Output: largest of 5 numbers Use function largest that takes the array as parameter and return the largest value

Finding Errors in Function Code Public static int sum(int x, int y) { int result; result = x+y;} int sum (int n) { if (n==0) return 0; else n+(n-1);} void f(float a); { float a; System.out.print(a); }

Method Overloading Method overloading: more than one method can have the same name But different signatures Argument lists could differ in – 1. Number of parameters. 2. Data type of parameters. 3. Sequence of Data type of parameters. Example: MyClass (int inputA, int inputB) MyClass (float inputA, float inputB)

Method Overloading The following method headings correctly overload the method methodXYZ: public void methodXYZ() public void methodXYZ(int x, double y) public void methodXYZ(double one, int y) public void methodXYZ(int x, double y, char ch)

Method Overloading public void methodABC(int x, double y) public int methodABC(int x, double y) Both these method headings have the same name and same formal parameter list These method headings to overload the method methodABC are incorrect In this case, the compiler will generate a syntax error Notice that the return types of these method headings are different

Method Overloading Case 1: int mymethod(int a, int b, float c) int mymethod(int var1, int var2, float var3) Result: Compile time error. Argument lists are exactly same. Both methods are having same number, data types and same sequence of data types in arguments.

Different Number of parameters in argument list

Difference in data type of arguments

Sequence of data type of arguments

Primitive VS. Reference Variables Primitive variables hold values of primitive data types. Example: int x = 5; x is primitive variable Instance variables hold references of objects: the location (memory address) of objects in memory. Example: int [] arr = {1,2,3,4,5}; arr is reference variable, it carries the address of the location of the array x 1 1100 arr 1 2 3 4 5 1100

A formal parameter receives a copy of its corresponding actual parameter

Scope of an Identifier Local identifier: identifier declared within a method or block, which is visible only within that method or block Java does not allow the nesting of methods; you cannot include the definition of one method in the body of another method Within a method or a block, an identifier must be declared before it can be used; a block is a set of statements enclosed within braces