Java Android |
What are the Methods ?
Applications and computer software are built by writing hundreds of lines of code, as it is known in any programming language. If we encounter any big problem, the best way to solve it is to divide it into a group of small parts or what is known as a module or "function" so that each of them performs a specific function, This technique is known as divide and conquer. And dividing the code into a number of functions makes the code easier to read and more clear.
This module or function functions are known in C # and the java with what is called a Method, and the programmer can write the Method to define specific tasks and then call them from any point of the program by simply mentioning their name in order to be implemented at that point.
The programmer either writes the method himself, then it is called ( user-defined method ), or he uses functions defined in the same language ( build in ), and all he has to do is call them when he needs them to use them in his program, and we will learn in the following how we can create these The user-defined method and then invoked in the program.
Method book of functions (Methods):
Now let's look at the general picture of writing any method in java, for example:
access_ Modifier return-value-type Method-Name( parameter-list )
{
declaration and statement
}
The first line of the definition of a method is called a method header and it contains :
Access modifier : These are either public, private, or static.
For example, when writing public, this means that all Activities can access the function, meaning that it is public. When private is written this means that the private function can only be accessed from within the current Activity.
method-name : It is the name of the function that we want to define, and the rules for writing the name here follow the same conditions for writing the names of the variables, and of course the name will be optional.
return-value-type The type of result to which the function will return. If it returns a value, it does not return more than one value. For example, if the function returns integer, we will write integer, and so on. In the case when the function does not return a value, we write here void.
parameter-list : are the values that we are sending or entering into for the function. You will get to know it more when we explain the types of functions.
As for the declarations and statements : they are the orders that this function executes.
Functions in any programming language have four types. I will give each type with an example application through the java language :
1- Function that does not return a value and does not receive any "parameter"
And the meaning will be clear through the following example:
public void firstMethod() {
Log.v("Method", "FirstMethod was called!!");
}
And here the access modifier is public, that is, public.
Since the function does not return a value, we will write void in return-value-type ...
Since the function does not receive any value, we will write the parentheses empty ...!
Here, the function performs a very easy function, just that it prints the message "!! FirstMethod was called " as soon as it is called .. And now that the function has been built, it must be called " calling " in order to print the message .. But how is any function called ??
Once the function name is mentioned, it will execute it ... as follows:
firstMethod();
And the whole program is as follows:
package com.example.android.methods;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//call the method
firstMethod();
}
public void firstMethod() {
Log.v("Method", "Method 1 was called!!");
}
}
Notice in the program that we first create the firstMethod function, then call it from within the onCreate as it is the starting point of the Activity. The result is that the phrase Method 1 was called!!
inside the Log is printed .
Now the second type of function type:
2 - The function that receives the value of "parameter" but does not return the value of :
And the following example illustrates the idea. To begin with, let's create the function as follows:
public void secondMethod(String name) {
String result;
result = "hello: " + name;
Log.v("second method", result);
}
The function here receives a parameter value, so we will write in parentheses the name of any variable that receives this value that will be entered into the function, and of course we will write a type for this variable and its type undoubtedly will be of the same type of the value to be passed to the function, which will be kept by the variable. As is evident, the function receives the value stored in name, then adds hello before the name, and finally prints the result to Log.
Just keep calling the function until it performs its work, but it seems that we must send a value for this function to do its work, and of course the value that we will send will be of type String….
And to call the function as follows:
secondMethod("Ahmad");
And the whole program is as follows:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//call the method
secondMethod("Ahmad");
}
public void secondMethod(String name) {
String result;
result = "hello: " + name;
Log.v("second method", result);
}
}
3- Functions that we do not pass parameters to but return with a value of :
The meaning that the function does not pass or receive a parameter is that when the function is created the parentheses will be empty (), meaning that there is no value that the function wants to receive.
And meaning that the function returns a value, i.e. when we create the function, we will use a keyword which is return and it is followed by the value to which the function returns. And do not forget that you must write in the Method header the type of value that the function will return in the field of return _value _type ...
Let's create the function and see, to first agree on the function of this function, for example we want the function to print the following sentence for us:
"third method was called"
The operation is very easy. First I'll write the function:
public String thirdMethod(String name) {
return "third method was called!!";
}
Here the function returns a value of type string, thirdMethod is called. We also used the word return followed by the value that the function will return. Of course, if we want to implement the function from any point in the program, all we have to do is just call it as follows:
thirdMethod ();
But the previous ThirdMethod function returns a value, so when we call it, we must store the value from which it returns in a variable, and this variable has the same type of return _value _type in the function itself, so we will know which variable has the type string in order to store the result of the ThirdMethod function so that the function call is correct as follows:
string result = thirdMethod();
Since we have stored the value that the function returns to in the variable result, then when you print the value result, you will print the result of the ThirdMethod function .
The full program:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//call the method
String result = thirdMethod();
}
public String thirdMethod(String name) {
return "third method was called!!";
}
}
4- Functions that receive parameter and return a value:
We want to write a function whose function is to return a value (which is the result of the calculation)
That is, the function receives an integer value, and it returns the integer integer, which is the number 10 multiplied by the value that was passed to the function.
What would the function look like:
public Integer fourthMethod(Integer number) {
return number * 10 ;
}
Since we are at the beginning of defining the function in the first line before the function name, we wrote integer, meaning that the function will return the integer value.
And we wrote (integer number), meaning that the function will receive an integer value stored in a variable called number
return 10 * number;
And the previous line means that the function will return the value 10 multiplied by the parameter sent to the function, and as we mentioned at the beginning of the lesson that the keyword return is used in order to return to us a value. Now that we have created the function, we keep calling it like this from inside the onCreate:
Integer result = fourthMethod(6);
Since we have sent the function a numerical value to be placed in place of the variable number, and of course the value 6 is optional. You can try any other number. And the function returns an integer value stored in the variable result. And the whole program is as follows:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//call the method
Integer result = fourthMethod(6);
Log.v("result", String.valueOf(result));
}
public Integer fourthMethod(Integer number) {
return number * 10 ;
}
}
Thus, we explained the difference between the four types of functions.
If you have any questions, do not hesitate to leave your comment. May the peace, mercy and blessings of God be upon you.
read also :
Comments
Post a Comment