Pages

Thursday, September 8, 2016

functions / methods in java

 Explanation

Till now as we know that, in java we have a main method called main function.But in java we also use some codes to do some separate task. So thats why here comes Method.

Difinition

A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. Think of a method as a subprogram that acts on data and often returns a value. Each method has its own name.
you can use many methods in a simple program.

parts of method

Parts of method mean's, In java method there are some parts ( codes ), Which java method is made.Like modifier, return part,name part,pass value and a body.Method is simply just made of these things.
Method condition
1.A method can't be in main function
2.If a method is an int method so it should has a return statement
3.If a method is void. So it's mean no return value should be use.
4.You can create many methods in a program
5.If a method is private so it's mean it can't be access out of Class
6.if a method is static so you can access it from anywher
7.You can call a Method many times.

Simple out put Method

So now here I am going to show you how to create a method without any return value andpassed value let's have a look at this example
 package myPackage;
 
 public class myClass{
 
 public static void main(String[]args){
  /*main function body*/
 }
 
 /*this is the method*/
 public static void myName(){
    /*I am in the method body*/
  System.out.println("Abdul wahab");
 
 }
 }
  

Example explanation

In this example myName is the name of the function.and in the function body as I print out "abdul wahab" so this means..that this method would just print out Abdul wahab.And now if you run this, So it won't work.Because we haven't called this method yet.So to call this method pretty simple.See this example
 package myPackage;
 
 public static void main(String[]args){
 /*
to call myName method just put the name 
with round braces*/

    myName();
 
}
  static void myName(){
 
 System.out.println("Abdul wahab");
 
 }
 
Now it will show something like this to the console if you are using eclips, As shown below
You can call a method many times its upto you as much as you need.Look at the Example
 package myPackage;
 
 public class myClass{
 
 public static void main(String[]args){
  /*call myName method three times*/
  
  myName();
  myName();
  myName();
 
 }
 static void myName(){
 
 System.out.println("Abdul wahab");
 }
 
 }
And it would show to the console as below

Method with String variable

You can use a variable also in a method.like below I am gonna show you a Stringvariable,How you can print out a String variable in methods.
package myPackage;

public class myClass{

 public static void main(String[]args){
 
 /*call your method right here*/
 
  myName(); 
 }
 static void myName(){
 
 /*create a String variable*/
 String a="my name is abdul wahab";
 
/*print out a String variable*/
 System.out.println(a);
 
 }
}

It would print out something like this.If you are using Eclips

passing a value to void method

Explanation

Passing a value to a void method is mean to put variables in the parameters of a method ie
package myPackage;

public class myClass{

public static void main(String[]args){

}
static void myMethod(int d){
/*Here we've passed an int to our
 method*/
}
static void myName(String name){
/*Here we have passed 
a String to our method*/

}
static void towValues(int height,int width){
/*You can pass many values
to a single method*/

}
}
So as you can see these are some simpleExamples about where we pass values in a method. Iam sure you got the idea!
.
As we are clear about how to passes values in methods.Now this is the time to know how to use them.Why we passes values in a method. I will make this pretty clear with some Examplesbelow
package myPackage;

public class myClass{
 
 public static void main(String[]args){
 
 /*call the method and put a
    number as a parameter*/
 myMethod(5);
 
 /*Call again with different value
 
 myMethod(88);
 }
static void myMethods(int number){
/*print out number variable*/
System.out.println(number);
}
}
What we did here? In the parameter of the method(myMethod).We just created an int variable named it number, .And then we print out the number variable. Then we called this method from the main function.and we passed a value 5 as a parameter. And then we recall the method and this time we passed a new value 88.And now if we run this so it would give us a result as shown below

passing many int variables in methods

You can pass many variable in a single methods.Just put commas in between each variable.ie
package myPackage;

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

/*call your method and put
 two values as you given a,b*/ 
 
 myMethod(8,8);
}
static void myMethod(int a,int b){

/*Here we passed two variable
let's add them with each other*/

System.out.println(a+b);
}
}
What you need to do, 1.Create a method. 2.pass two int variable a and b. 3.In print out add them. 4.And call them in main,and make sure to give some values other wise it cannot proccess with a and b. I put (8,8). 5.And that set.
And now if you run this so it should be like this

passing String in method

Explanation

You can pass a String variable as well into the parameter of a me thod.Like in below ExampleI am gonna be passing a String variable to the method as a parameter
what you have to do is just you have to put String instead of int ie
package myPackage;
public class myClass{
 public static void main(String[]args){
 
 /*call your method*/
 
 myMethod("abdul wahab");
 
 /*put your name in double quotes
 as a parameter*/
 }
static void myMethod(String name){
/*print out name*/

System.out.println(name);
}
}
What we have done here? We just put a String variable into the method as a parameter,named it number. and then we print out we then called this method from the main function. And we gave value as "abdul wahab".Now if you run this so it would look like the same as shown below

Passing String and int both in a method

You can pass all types of variables an int,String and double etc in a single method.Have a look at the Example below
 package myPackage; 
 public class myClass{
  
 public static void main(String[]arga){
   
    myMethod(String health,int age){
   
   if(health=="good"){
  
  System.out.println("congrats you can come");
  
  /*if statement is in another if statement*/
  
  if(age>33){
     
  System.out.println("you are old");
}
else{
  System.out.println("you are out");
  }
 }
else{
   System.out.println("you can come");
    
   } 
  }
  }
Here we passed two different types of variables in a single method.And it works perfect and fine.So its mean you can pass many types of variable in a single method

Methods with return type

There is another type of method in java programming.In which we get a value back as return.Let's have a look at the example below.While in this example I am gonna be returning an int back to my main function.ie
package myPackage;

public class myClass{
 public static void main(String[]args){
 /*call the method in print out 
 statement*/
 System.out.println(myMethod());
 }
 static int myMethod(){
 /*return 8 as an int*/
 
 return 8;
 }

}
As you saw we created an int method in which we return a value 8 and then we called this return value from the main method and now if we run this so it would print out

Return String method

You can create a String method as well in java programming which will return a value back to you as shown below
package myPackage;
public class myMehtod{
 public static void main(String[]args){
 
 /*call the method*/
 System.out.println(myMethod());
 }
 static String myMethod(){
 
 /*assign the value of String as
 how are you?*/
 
  return "how are you?";
 }
}
I hope it makes scence to you.method is the same.We just put String instead of an int, and if you run this so it would print out

Passing a value to the void method

As i discussed already there are some parts of a method modifier, return type,name of method , parameter list and a body ofcource. Now here we'll go over what a parameter list is and how to use it in a method.So first of all the parameter list can be known as the codes in the round braces.ie
package myPackage;

public class myClass{
 public static void main(String[]args){
 
 }
 static void myMehtod(){
  
  /*after the myMethod the round brases()
  is called parameter list*/
  
 }
}
So now as you got the idea about what a parameter list.Now this is the time for how to use.So for this let see this example first
package myPackage;

public class myClass{
 public static void main(String[]args){
  
  /*call the function 
  and pass in an int value
 myMethod(8);
 }
 /*create an int and name it
 number*/
 static void myMehtod(int number){
 /*print out number*/
 System.out.println(number);
 }

}
After run this is the result for you
So what we did here, we createed a parameter list with an int named number..And then in the method body we printed out the number.And then we called this method up in the main function. But (notice this) make sure.It will take an int value as a parameter.As i put 8 in theexample above.
As well as you can pass two values or many values in a single method ie
 package myPackage;
 
 public class myClass{
 public static void main(String[]args){
 /*call the method and
 pass three values into 
 the method,as you mentioned
 (a,b,c)in the method(myMethod)*/
 
 myMethod(10,4,4);
 
 }
  static void myMethod(int a,int b,int c){
 /*Here we passed three values
 in a single method*/
 
 System.out.println(a+b-c);
 /*
 this will add first two values
 while it will then 
 subtract the last one 
 from thier total*/
 }
 }
 
As a result you will get

Passing value using different data types in a method

You can pass different data types in a singlie method.What this mean is actually if you need a method in which you wanna use all the data types like Stringint and double etc.So yeah let's see an example for to make yourself clear about this.
  package myPackage;
  public class myClass{
  public static void main(String[]args){
  /*call the method
  and give the values*/
  myMethod("your total is ",10,2.8);
  }
  static void myMehtod
    (String a,int b,double c){
  /*Here we used 3
  data types in a single
  method*/
  System.out.println(a+(b-c));
  /*subtract c from b*/
  
  }
  
Now let's run this

Passing values into return type method

To pass a value into return type method is also the same as we did in void but this time the method is change.In this method we'll get return something, to return something put return at the end of your method, otherwise it wont work ,and also put int instead of void..ie
package myPackage;
public class myClass{
public static void main(String args){

/*call the method 
in out put statement
and pass a value as a 
parameter*/

System.out.println(myMethod(8));
}
public static int myMethod(int number){
  
  /*return the number*/
  
  return number;
}
}
Here we have a method,which is obviosly a return type,Now in this method we passed an int variable (name=number) as a parameter.And then we return the number.And in the main method we printed out this method with value 8.Now let's run and see!
You can pass and return two or more than two values as well like below
package myPackage;

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

/*print out myMethod
and pass two values
for the height and width*/

System.out.println(myMethod(3,9));
}
public static int myMethod(int height,int width){
 
 /*return the multiplication
 of height and width*/
return height*width;
}
}
 
And if you run it would show
So this was a simple example i am sure nothing here to explain.But if you really want to know more about this.leave me a comments below.

Use of Scanner Object in a return type method

Now this is another Example and in this Example am gonna be showing you using of Scanner object in a return type method
 package myPackage;  

import java.util.Scanner; 
public class myClass{
 
 public static void main(String[]args){
 /*create a scanner object*/
 
  Scanner d= new Scanner(System.in);
  System.out.println("types in three numbers")
  
  /*get first input in a*/
  
  int a= d.nextInt();
  
  /*get second input in b*/
  
  int b=d.nextInt();
  
  /*get third input in c*/
  
  int c=d.nextInt();
 
  
  /*now call the method
  and pass all the three values
  you got from the user
  into the parameter*/
  
  myMethod(a,b,c);
 }
 static int myMethod(int a,int b,int c){ 
   
  
  /*use an if statement*/
  
if(a>b && b>c){
 System.out.println("b is bigger");

 }else if(b>a && b>c){

 System.out.println("b is bigger"); 

}else{

System.out.println("c is begger");
}  

/*and now return nothing 
because we don't need
something to return*/

return 0;
 }   

  }
And now if you run this
So yeah that's all for a method....If you have any problem belong to this topic ,or related to java you can leave me a comment and i would answer all your questions in very short time
If right now if you have any problem just contact me i have given the contact form in each of my page.So just click on that and leave me a comment or just message me and trust me.i'd love help you out from every difficulties you have..

No comments:

Post a Comment