0% found this document useful (0 votes)
14 views46 pages

Gr 9 Ls 6 Methods & functions icse

The document provides an overview of methods in programming, detailing their definition, advantages, and components. It explains how to define, invoke, and categorize methods, including static and non-static types, as well as the use of return statements and parameters. Additionally, it covers variable types and constructors within classes, emphasizing the importance of methods in code reusability and modularity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views46 pages

Gr 9 Ls 6 Methods & functions icse

The document provides an overview of methods in programming, detailing their definition, advantages, and components. It explains how to define, invoke, and categorize methods, including static and non-static types, as well as the use of return statements and parameters. Additionally, it covers variable types and constructors within classes, emphasizing the importance of methods in code reusability and modularity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Methods /

Functions
Ls 6
Definition

A method is a named piece


of code within a program
and executes when it is
called from another section
of code.
Advantages
*Reusability of code
*Modularity. Divides a complex task
into a collection of smaller
methods
*Abstraction hides implementation
details from the component using
the method.
*Occupies less memory
*Executes faster
Defining a Method
*The general format of defining a method is
shown below
<access-specifier><returntype><method
name>(parameterlist)
{ --------------------}
Eg: public int add(int m,int n)
{
int k = m+n;
return(k);
}
Construct or
components
of a method
*Methods live inside classes
*A java program can have
many classes
*Each class can have many
methods
Method Header /
prototype
*Access specifier
*Return Type
*Function name
*Parameter List
*public int add(int m, int n)
Method Header /
prototype
*First line of the function
definition that tells the
program about the type
of value returned by the
method and the number
and type of arguments
Examples for function prototype
*int absval(int a)
*public int maximum(int a, int b)
Method
*Method name
Signature
along with the
parameters
*Add(int a,int b)
Method Types

*Static methods
*Non static
methods
Static Methods

*Methods having static keyword


in the prototype are called
static methods.
*Eg:
public static void main(String
args[])
{………}
*Non static
methods
*Methods which do not have
the static keyword are called
non – static methods
*Eg
public int add(int a,int b)
{……..}
Invoking a method
*A process of using
a method in a
program is referred
to as Calling a
method or Invoking
a method.
Invoking a non static
*Create anmethod
object of the class
*Call the method using the created
object.
*Eg: Sum ob=new Sum();
ob.Add(a,b);
Sum – Class name
ob- object name
Add- Method name
a,b - parameters
Invoking a static method
*In case the method is
static, then main() and sub
methods are compatible to
communicate with each
other.
*No need to create the
object for its calling
*int p=Add(a,b);
Invoking a static method
*Static methods are called
using class’ name outside
the class e.g Math.pow()
*Static methods are called
directly without using any
class name inside the
class’ methods eg int
p=Add(a,b);
Method invocation
*Whenever a method call
statement is encountered , the
control is transferred to the
method
*The statements in the method
body are executed
*Control returns to the
statement following the
function call
Use of void as the return
type
If a method is not returning
anything its return type is
specified as void
public void add()
{int j=2*5;
System.out.println(j);}
*If return type is void ,
function cannot be used in
an assignment statement.
*Only methods returning
some value can be used in
expressions and
assignment statements
Method main() in
java
* If a program has multiple
classes , then only one class
would contain main()
Access Specifier

*Public
*Private
*Protected
Parameters

Formal parameters
Actual parameters
Formal Parameters

*The parameters described


in the method header
which receives values from
its caller method are called
formal parameters
*Eg: int add(int a, int b)
{----}
Actual Parameters

*The parameters used in a


function call are called
actual parameters or
arguments
*Eg: add(10,20){----}
Features of return
statement
*Always used at the end of the
method.
*No statement in the method block
will be executed after the return
statement.
*Return statement can only return a
single value from a method to its
caller.
*A function may have more than one
termination points. Thus, a number
of return statements may be
Features of return
*In statement
case multiple return
statements are implied in a
method, only one of them will
be executed to return the
control
*Once control exits from a
method, it can’t reappear into
the method block for any
further execution, unless the
Uses of return
statement

*To cause an immediate exit


from the method/function
*Used to return a value to
the calling code
Different ways of defining
a function
Functions receiving value and
returning outcome to the caller
•Eg: Function call:
int k=Obj.add(5,2);
Function definition
public int add(int m,int n)
{int j=m+n;
return(j);}
Functions receiving value but
not returning outcome to the
caller
•Eg: Function call:
Obj.add(5,2);
Function definition
public void add(int m,int n)
{int j=m+n;
System.out.println(j);}
Functions neither receiving
value nor returning outcome to
the caller
•Eg: Function call:
Obj.add();
Function definition
public void add()
{int j=2*5;
System.out.println(j);}
Types of variables

*Instance variables
*Parameter
variables
*Local variables
*Instance variables
*Instance variables are declared within
the class but outside the member
method.
*When a number of objects are
created for the same class , the same
set of instance variables are used for
all.
*Instance variables are also referred
to as fields of an object
*Instance variables belonging to
different objects contain different
Class variables / static variables

*Class variables are


declared within the class
but outside the member
method along with static
keyword.
*A variable declared static
will be available as a single
copy to be used by all
objects of a class.
*Local variables
*A variable that is accessible only in a
specific function or a block in which
it is declared is called a local variable
*Itis declared within the body of a
method
*A local variable can never be a static
variable
*The scope of these variables is limited
within curly braces of a method in
which they are declared
*They are not given initial default
values .
Local variable
example
public void add(int m,int
n)
{ int j=m+n;
System.out.println(j);}
//here j is an example for a
local variable
Constructor
*These are member methods
used to initialize instance
variables.
*They possess the same
name as that of the class
*class Example
{
int a,b;
Example(){ a=0; b=0;}
*Constructor is automatically
called while creating an
object.
*Constructor must have no
explicit return type
*Constructor is always public
*Constructor is overloaded
automatically
THANK YOU

You might also like