Martrinex Learning Center
HOME CONTENTS
 
This website is underconstruction.
Author: Martin Sykes
Challenge: I have challenged myself to 1 article per day, and framework improvements! Enjoy
Challenge end: unknown

Functions


We have used functions already, this tutorial will show how to make them in order to seperate your code into more manageable sections. A function runs a block of code and usually returns an answer. (though in Java it can return void/nothing known as a sub)

We use the function println (System.out.println) to run code and print text to the console, or stdin.readLine() to get a line of text from the keyboard, or a better example String.toLowerCase() to remove capitols from text!
Format:
Code:
/* when writing your own functions do not put them inside other functions!, on other words create it outside of the
public static void main(){


}
// heres good...
*/

[variable type] [function name] ([parameter type 1][parameter 1],[parameter type 2][parameter 2],etc){

/* code to be run here */
return [variable type][answer]; //<-- gives the answer back
}
[variable type] = what format the function will return: void (nothing), int, long, double, bool etc...
[function name] = an informative name given to the function, names have same rules as variables
[parameter type][parameter] = you can send information to the function to work with, this is known as a parameter, in order to let the user send parameters you must declare them first inside the functions bracks (). You will see what I meen in the sample below.
Sample:
Code:
int answer=addNum(3,5); // call our new function!
System.out.println(""+answer); // display the answer...
// new declare the function
int addNum(int num1, int num2){

// note it takes 2 intergers (num1 and num2) (3 and 5)
return num1+num2;
// it returns (gives back) an integer (3+5) = 8
}
This small sample function 'addNum' adds to integers together and gives the answer...

Excersizes:


A few short tests to allow you to get the hand of functions :-)
Task 1
Using the code from 'addNum' create a new function named 'mulNum' which takes two integers a multiplies them together, (you may use '*' instead of plus) giving the answer... because multiplying creates significantly larger numbers, convert the function to return a long instead of int (see [variable type] above)
[click for answer]
Task 2
Using the code from 'addNum' add a new parameter num3 and make the function add up 3 numbers num1,num2,num3 and return the result... call this 'add3Nums'
[click for answer]
Task 3
Create a new function named 'logon' which takes 2 parameters (String forename, String surname) uses if(String.compareTo()==0) to check if it is your forename and surname and return bool, true if it is and false if it isn't, you may want to recap on: the 'if' statement regarding comparing strings.
[click for answer]

Add Comment

Help the web master by commenting on the tutorials, thankyou.
Name:
Email: (this will not be shown)
Comment:
  Send email to admin, don't post.

 

Comments

Nobody has commented on this article.
Be the first, the webmaster needs comments good or bad to improve this site.



Copyright (c) 2008, Martin Sykes.
Learning Center is a branch of Martrinex Systems, Martrinex.net
Do not copy any materials from this site without permission from the auther.
Martrinex Learning Center[X]
  Introduction Beginner Intermediate

Important useful well commented source codes
Please read the import tutorial to know how to use them.

Source