Skip to content

Instantly share code, notes, and snippets.

@odedlaz
Last active November 27, 2015 19:04
Show Gist options
  • Save odedlaz/85c8c22cb91fb5678d46 to your computer and use it in GitHub Desktop.
Save odedlaz/85c8c22cb91fb5678d46 to your computer and use it in GitHub Desktop.
Explanation how functions work
import java.util.Arrays;
public class Functions {
public static int powerOfTwo(int x) {
int num = x*x;
System.out.printf("%d^2: %d\n", x, num);
return x*x;
}
public static int addFive(int num) {
num += 5;
return num;
}
public static int[] copyArray(int[] arr) {
int copy[] = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
copy[i] = arr[i]; // copied!
}
return copy;
}
public static int[] addFive(int[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i] += 5;
}
return arr;
}
public static int power(int base, int exponent) {
int num = base;
// why exponent - 1?
// we are using a zero index.
// if we started with i = 1, than the stop term would be: i < exponent
for (int i = 0; i < exponent - 1; i++) {
/* wow. what's that?
* I'm only multiplying base 'exponent' times
* until I get the number I wanted
*/
num = num * base; // can also write: num *= base
}
// I'm returning base now
System.out.printf("base^exponent (%d^%d): %d\n", base,exponent,num);
return num;
}
public static void main (String[] args) {
/* lets play with functions a bit
* If I wanted to take a a number and square it,
* which means, for every x we'll return x*x.
* this is really easy:
*/
int x = 5;
int xSquared = x*x;
System.out.printf("x: %d | x**2: %d\n", x, xSquared);
// this is really nice, but I want to do the same thing again
// lets do that for y:
int y = 7;
int ySquared = y*y;
System.out.printf("y: %d | y**2: %d\n", y, ySquared);
// do you see the repition there?
// I can encapusulate that (fancy word for moving a bunch of code to a function)
// lets write the square function above!
int zSquared = powerOfTwo(4);
/* ok, that's really nice
* but what if we wanted to write our very own power function?
* power of two is really the number times itself two times
* power of three is really the number times itself three times
* so lets write the power function above
*/
int num1 = power(2,3); // multiplying 2 three times
/* we just created our very own power function
* keep in mind that java has its own set of common used functions
* split into different "packages" called: java.util, java.math, java.lang
* and more. we call these the "Java Standard Library"
* in c++ for example, it's called "std"
many languages come with a standard library,
also called "battaries included"
*/
// ok, now for the last part.
// what happens to our variable when we call a function?
// lets create the "add + 5" function
int num2 = 5;
int num2PlusFive = addFive(num2);
// look inside "addFive"
// it got the num variable and added another value into it
// so num2 should be equal to num2PlusFive right?
// try to think about the result before you run this code
System.out.printf("num2(%d) == num2PlusFive(%d)? %s\n", num2,
num2PlusFive,
num2 == num2PlusFive);
// java always copies variable "by value", which means that
// when we passed the num2 variable to the addFive function
// it copied it and didn't change the original
// when we returned it, it actually returned a copy!
// lets do the same for an array
int[] numArray = new int[] { 1, 2, 3}; // this is a short notation to initialize
int[] numArrayPlusFive = addFive(numArray);
// now what happens?
System.out.printf("numArray(%s) == numArrayPlusFive(%s)? %s\n", Arrays.toString(numArray),
Arrays.toString(numArrayPlusFive),
Arrays.equals(numArray,numArrayPlusFive));
// the reason behind it is that java passed the origin variable, not copying it
// the difference is that arrays are not primitive types (int,short,double...)
// so java passes the array itself and doesn't copy the entire array
// this is very important for the image editing functions
// because they should NOT change the value passed to them... only work on a copy.
// by the way, what I just said is kind of a lie
// the next few lines are more advance, so if you don't understand anything
// and prefer not googling "pointers", "reference type vs value type",
// "pass by reference vs pass by value", "stack vs heap", just read the next line:
// if your variable is initialized with the "new" keyword -> it doesn't get copied.
// what really happens behind the scenes is that when you create an array
// you use the "new" keyword, which means the array is initialized on the heap
// and when you pass a non primitive to a function, java copies the
// the pointer of that variable! java still always copies by value
// but when dealing with non primitives (aka reference types)
// only the pointers are copied. which means you actually get a copy
// of the address to the variable, but not a copy of the entire memory
// representation of that variable.
// if you have a primitive, which is initialized on the stack,
// java copies the variable.
// if we want to create a new array and not change the original
// we need to copy it! I pass the copied array now...
int[] numArrayPlusFiveCopy = addFive(copyArray(numArray));
// now what happens?
System.out.printf("numArray(%s) == numArrayPlusFiveCopy(%s)? %s\n", Arrays.toString(numArray),
Arrays.toString(numArrayPlusFiveCopy),
Arrays.equals(numArray,numArrayPlusFiveCopy));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment