0% found this document useful (0 votes)
17 views7 pages

Functions in Java

The document provides an overview of functions and methods in Java, including predefined and user-defined functions. It explains the syntax for defining methods, calling user-defined methods, and demonstrates examples of methods with return types. Additionally, it covers the use of parameters and access specifiers in method definitions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

Functions in Java

The document provides an overview of functions and methods in Java, including predefined and user-defined functions. It explains the syntax for defining methods, calling user-defined methods, and demonstrates examples of methods with return types. Additionally, it covers the use of parameters and access specifiers in method definitions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Functions/Methods

------------------------------------
println()

nextInt()
length()
CharAt()

length()

"Besant technologies"

---->Methods is a block of code that perform some specific task.

Employee management.

Wipro,TCS,...

login()
logout()
-------------------------------------
types:
----------
1)predefined function
2)user defined function

1)predefined function
----------------------------------------
---->These Functions are defined by java library.

2)user defined function


-----------------------------------------
These functions are created by user requirement

main method
------------------------
public static void main(String[] args)
{

syntax:
----------------
public static datatype method_name(list of arg)
{
//statements
}

public---->access specifier
static --->keyword
datatype---->int,float,void,char

void---->does not return any value


int,float,char,..--->it returns any value

method_name ---->valid name


list_of arg --->parameter.

public class Myclass3


{
public static void main(String[] args)
{
System.out.println("Welcome to Main method")
}

public static void mymethod1()


{
System.out.println("This is User Defined Method");
}

Output
-------------
Welcome to Main method

How to calling a userdefined method?


---------------------------------------------------------------
methodname();

EX:
-----
public class Myclass3
{
public static void main(String[] args)
{
System.out.println("Welcome to Main method")
}

public static void mymethod1()


{
System.out.println("This is User Defined Method");
}

}
-----------------------------------------------------------------------------
public class Main
{
public static void main(String[] args)
{
mymethod2();
System.out.println("Hello World");
mymethod1();//calling a method
}

public static void mymethod1()


{
System.out.println("This is userdefined method");
}

public static void mymethod2()


{
System.out.println("This is method2");

}
}
-------------------------------------------------------------
public class Main
{
public static void main(String[] args)
{
add();
}

public static void add()


{
int x=100,y=200;//functional variable

System.out.println(x+y);

}
}
-------------------------------------------------------------------
package Mypack1;

import java.util.Scanner;

public class Myclass3


{
public static void main(String[] args)
{

System.out.println("This is main method");


add(45,89);
add(5,3);
add(9,6);

public static void add(int x,int y)//x=45,y=89


{

System.out.println(x+y);
}

-------------------------------------------------------------------------
package Mypack1;

import java.util.Scanner;

public class Myclass3


{
public static void main(String[] args)
{
Eligible_to_vote(55);
Eligible_to_vote(12);

public static void Eligible_to_vote(int age)


{
if(age>=18)
{
System.out.println("Eligible for voting...");
}

else
{
System.out.println("Not Eligible for voting");
}
}

---------------------------------------------------------------------
package Mypack1;

import java.util.Scanner;

public class Myclass3


{
public static void main(String[] args)
{

int a;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the value");


a=sc.nextInt();

Eligible_to_vote(a);
}

public static void Eligible_to_vote(int age)


{
if(age>=18)
{
System.out.println("Eligible for voting...");
}

else
{
System.out.println("Not Eligible for voting");
}
}

---------------------------------------------------------------------
return type
-----------------------
syntax:
----------------
return value;

EX:
-----
package Mypack1;

import java.util.Scanner;

public class Myclass3


{
public static void main(String[] args)
{

cube(10);//calling function

System.out.println("The value is:"+cube(10));


}

public static int cube(int n)


{
return n*n*n;//return 10*10*10-->return 1000
}
}
EX:
-----
package Mypack1;

import java.util.Scanner;

public class Myclass3


{
public static void main(String[] args)
{

Mymethod1(12.3f);

System.out.println("The Result is "+Mymethod1(12.3f));

public static float Mymethod1(float n)


{
return n*8;//
}
}

----------------------------------
EX:
----
package Mypack1;

import java.util.Scanner;

public class Myclass3


{
public static void main(String[] args)
{

Mymethod1(3);

System.out.println("The Result is "+Mymethod1(3));

public static float Mymethod1(int n)


{
return n*8;//
}
}

You might also like