Instantly share code, notes, and snippets.

Embed
What would you like to do?
Supporting gist for post 'functions'
/*
* 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;
}
}
/*
* 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!
}
}
/**
* 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