0% found this document useful (0 votes)
2 views

Ch08Functions(Methods)

Chapter 8 discusses functions and methods in programming, defining a function as a reusable code block that enhances modularity and code reuse. It covers the need for functions, types of functions, function definitions, and various concepts such as calling functions, parameters, getter/setter methods, and constructors. Additionally, it explains the differences between pure and impure methods, call by value vs call by reference, and the use of the 'this' keyword.

Uploaded by

rivandhingra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Ch08Functions(Methods)

Chapter 8 discusses functions and methods in programming, defining a function as a reusable code block that enhances modularity and code reuse. It covers the need for functions, types of functions, function definitions, and various concepts such as calling functions, parameters, getter/setter methods, and constructors. Additionally, it explains the differences between pure and impure methods, call by value vs call by reference, and the use of the 'this' keyword.

Uploaded by

rivandhingra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Ch.

08: FUNCTIONS/METHODS
8.1 Function
• A function is a block of organized and reusable code that is used to perform a specific
task.
• Functions provide better modularity for your application and a high degree of code
reusing.

8.2 Need of a function


• To divide a large complex problem into smaller sub problems.
• To hide low-level details.
• To reuse portion of code.

8.3 Types of function


• Built-in functions: Which are already defined in Java Library classes.
e.g. : pow(), nextInt(), charAt() etc.
• User defined functions: Which are defined by the programmer.

8.4 Function Definition


The general syntax to define a method is:
[Access modifier] [Non-Access modifier] <return type> <function-name>([Arguments])
{
// body of the function
}
[ ]  Optional
< >  mandatory
• Access specifier: can be either public or private or protected.
• Non-access modifiers: final, static, abstract etc.
• Return type: Specifies the type of value that the return statement of the function
returns.
• Function’s name: User defined and should be legal identifier.
• Parameter list: list of comma-separated variables, a function referred to as the
arguments or parameters.
• Body of the function: Depicts the working of a method.

8.5 Function Prototype vs Function Signature vs Type Signature


e.g. for the given definition of a function sum()

int sum(int a , int b)


{
return (a+b);
}
• Function Prototype: Function prototype is a declaration of a function that specifies the
return type and function signature.
It is the first line of the function definition.
• Function signature: A function signature usually includes the function name , and the
number, types and order of its parameters.

8.6 Calling/Accessing a function: A function can be invoked/called/executed by using


function’s name followed by matching arguments.
e.g. // function definition
void sum( int a, int b)
{
System.out.println(“Sum = “+(a+b));
}
// calling statement
sum(10,15);

8.7 Formal Parameters vs Actual Parameters


• Formal Parameters: The parameters that appears in function definition are called formal
parameters.
• Actual Parameters: The parameters that appears in function call are called Actual
parameters.
8.8 Getter Methods vs Setter Methods
• Getter/ Accessor Method: It is a method that returns the value of a data member of the
class. These are generally provided for private members.
• Setter/ Mutator Method: It is a method that set/changes the value of a data member of
the class.
public class student
{
private int Rollno;
String name;
// Getter Method
int getRoll()
{
return Rollno;
}
//Setter Method
void setValues()
{
Rollno = 1;
name=“Aakash”;
}
}

8.9 Pure Methods vs Impure Methods


• Pure Method: It is a method that never change the state of its argument.
• Impure Method: It is a method that changes the State/value of its argument.

8.10 Call by value vs Call by reference


Call by Value Call by Reference
• In call by values , any change made in • In call by reference , any change made
formal parameters doesn’t reflect back in formal parameters reflects back to
to the actual arguments. the actual arguments.
• In call by values, as the copy of the • In call by reference, no extra storage is
argument is passed hence it requires required as the reference of the actual
extra storage. argument is passed.
• In Java programming, all primitive • In Java programming, all reference
types are passed by value. types (like array , object) are passed by
reference.
8.11 Returning from a function
A function, after execution ,Generally returns to the calling statement in two ways:
1.When a return statement occurs.
int sum( int a , int b)
{
return (a+b);
System.out.println(“sum =“+(a+b));
}

2.When the last statement of the body of a function executed.


void sum( int a , int b)
{
int s=a+b;
System.out.println(“sum =“+s);
}

8.12 Calling a method


Case 1: When a method doesn’t return a value.
If a function doesn’t return a values then the return type should be ‘void’.
class sample
{
int n;
public sample ( int nn)
{
n = nn;
}
public void factorial ( )
{
int f=1;
for ( int i =1; i<=n;i++)
{
f = f*i;
}
System.out.println(“Factorial = “ + f);
}
public static void main( int num)
{
sample ob = new sample(num);

ob.factorial(); // calling a method that doesn’t return a value


}
}//end of class

Case 2. When a method returns a value.


If a function returns a values then the return type should be the type of the value the function
returns.

Invoking : To invoke this type of function, calling statement can be written in following 3 ways:
1. As the condition for a control statement (if , while etc.).
e.g. if (<function-name>(arguments) )

2. As an assignment to a variable.
e.g. Variable = <function-name>(arguments);

3. In printing statement
e.g. System.out.println (<function-name>(arguments) );
e.g.
class sample
{
int n;
public sample ( int nn)
{
n = nn;
}
public int factorial ( )
{
int f=1;
for ( int i =1; i<=n;i++)
{
f = f*i;
}
return f;
}
public static void main( int num)
{
sample ob = new sample(num);
int fact=ob.factorial(); // calling as an assignment
System.out.println(“Factorial =“+fact);
System.out.println(“Factorial =“+ob.factorial()); // In printing statement
if(ob.factorial() == 120) // Calling as a condition
System.out.print(“number is 5”);
}
}//end of class
8.13 The “this” Keyword
The “this” reference stores the address of current–calling object.
The keyword “this”
1. only pertains to an instance method, not to a class/static method.
2. is accessible inside instance method.
3. distinguish between instance variable and local variable ( if these have same name and
used in same block).
class Abc
{
int n; // instance variable
public Abc (int n)
{
this.n = n; // “this.n” represents instance variable.
}
}

8.14: Constructors A constructor initializes an object, with some legal values, immediately
upon creation.
Properties of a constructor:
1. It has same name as of the class name.
2. It doesn’t have any return type not even void.
3. It invoked automatically as soon as the object is created.
Types of Constructors
• Default constructor: Constructor that doesn’t have parameter list.
• Parameterized constructor: Constructor that has parameter list.
• Copy constructor: Constructor that has an object as the parameter. It is used to initialize
an object with values of an existing object of the same class.
e.g.
public class Sample
{
int a; // instance(object) variable
public Sample() // default constructor
{
a=0;
}
public sample( int x) // Parameterized constructor
{
a=x;
}
public sample(Sample ob) // Copy constructor
{
a=ob.a;
}
public void input() // Member function
{
a=10;
}
public static void main ()
{
Sample ob1 = new Sample(); // To invoke default constructor
Sample ob2 = new Sample(8); // To invoke Parameterized constructor
Sample ob3 = new Sample(ob2); //To invoke Copy constructor
ob1.input(); // To invoke input( ) function
}
}

You might also like