Hello! I am just learning java from basics these days. Can someone guide me here how to write functions in java with demonstrations like function for adding two numbers or function for finding the maximum number in an array which gives rise to another question; how to declare an array in java? Please explain about the prototypes and return types of the functions in your explanation as well. Thank You!
Writing basic functions in Java
Happy to support you!
Adding two numbers in Java is easy.
Example for adding numbers-
Declaration of arrays is simple in Java, through the use of either
-
Array declaration
2. Array literal
Finding the maximum value in an array can be understood by studying the following codes,
————————————————————————————————————————
-
Prototype function in Java –
-
Return type functions in Java-
Writing basic functions in Java
A JavaScript function is a set of JavaScript codes written and stored in a JS file “.js”. A code is inserted inside the heading tag <head></head> to link the JS file to the page. The JavaScript code looks like this: <script src="http://www.domain.com/partners.js"></script>. Though the JS file is linked to the page, the functions inside the file will not start automatically when the page starts loading.
Every function needs to be called manually or through another function which is like nesting. To call a function, you need to specify the label of the function. For example: <script type="text/javascript">ContentCooperation()</script>. To add 2 numbers or to perform addition in a function, see the example below:
function Adding(){
Value = 2 + 2
document.write(Value)
}
Here, the label of the function is Adding(). The variable Value is declared and contains “2 + 2”. When the computer sees the plus sign between the numbers, it means addition and it will add the 2 numbers putting the result in the variable Value. So, the value of Value is “4”. When the computer reaches “document.write”, it will print the value stored in the variable Value.
The symbol “{}” starts and terminates the function. To print the value of the function in the page or to call the function, insert the code <script type="text/javascript">Adding()</script> anywhere in the page where you want the result to appear.