Created
November 9, 2015 13:49
-
-
Save OOPUniversity/828d35682f48087b3cc4 to your computer and use it in GitHub Desktop.
Supporting gist for post 'functions'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2014. | |
*/ | |
package main.java.com.oopuniversity.basics.functions; | |
/** | |
* Created by OOPUniversity on 11/15/2014. | |
*/ | |
public class Add { | |
public static void main(String[] args) { | |
int answer = add ( 2 , 3 ); | |
System.out.println("The answer is " + answer ); | |
} | |
/** | |
* About as basic a function as one can create, Add accepts two 'parameters' | |
* and uses them to do a simple calculation. | |
* | |
* @param number1 | |
* @param number2 | |
* @return integer containing the result of adding number1 to number2 | |
*/ | |
public static int add ( int number1, int number2 ) { | |
return number1 + number2; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2014. | |
*/ | |
package main.java.com.oopuniversity.basics.functions; | |
/** | |
* Created by OOPUniversity on 11/15/2014. | |
*/ | |
public class BadAdd { | |
public static void main(String[] args) { | |
int answer = badAdd ( 2 , 3 ); | |
System.out.println("The answer is " + answer ); | |
} | |
/** | |
* A demonstration of how NOT to write a function. Since this will not | |
* always calculate a return value, the compiler won't accept it as valid | |
* Java code. | |
* | |
* @param value1 | |
* @param value2 | |
* @return Absolutely nothing, since this fails to even compile as written | |
*/ | |
static int badAdd( int value1, int value2 ) { | |
if ( value1 < 100 ) { | |
return value1 + value2; | |
} | |
//Fails to compile! | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by OOPUniversity on 11/9/2015. | |
*/ | |
package main.java.com.oopuniversity.basics.functions; | |
/** | |
* | |
* This package outlines some basics of writing valid (and invalid) functions | |
* in the Java language. We have one working and one non-working example, showing | |
* how an 'Add' function could be created. | |
* | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment