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