In today's class, we learned about functions.
In Java, which is an object-oriented language, all functions are methods inside an object. We can create a function by using reserved keywords and a function name, as well as arguments and a return type.
When using functions, we have to be careful of the scope of the variables we're using. We also have to watch out for using arrays in our arguments - this is the difference between passing by value and passing by reference.
When we use an array as a parameter in the function, whatever values we tweak inside will be changed in the array - if we have a method that doubles every integer in an int[], the actual values (int[0], int[1], etc.) will be changed. On the flip side, when we pass a primitive type the variable values won't change, as they are passed by value. To illustrate this, imagine a method that doubles an int. If we pass a variable i with a value of 6 into the method and then print it out after, the value will still be 6. https://replit.com/@offexe/functions/
We also learned how to write functions and the parts of a function, such as its scope and its return type.
Homework: https://docs.google.com/forms/d/e/1FAIpQLScGRPOVFEIuzp1pKiviZBx7tK_etkQia8slVpQ7AKdS03X6KA/viewform?usp=sf_link
If you would like to continue learning Java, consider taking the ACSL or APCSA courses with us.