Hello guys, I am back with the 2nd article of my Java Fundamentals article series. Today I am going to discuss Functions and Parameters.
What is a Function?
Large programs that contain large codebases are complex. So, understanding the code and debugging is harder. The best answer we have for this issue is breaking down the code into small units. After breaking down the code into units, we can call and invoke that each piece of code calling by their name.
These small units are called functions. When we call a function by its name, we can invoke whatever the code that is inside the function body. When I say this, the first thing that would come to your mind as a programmer is, “Yes, but methods does the same thing, right?”. Well, yes, you are right. Let me clear the confusions.
Functions vs Methods
What both functions and methods do are identical. They can take in values as arguments and can return values.
But the difference between these two is that a function is an individual unit. A function can exist on its own without any help.
public class Test { public static void main(String[] args) { //Calling the function myFunction(5); //Calling the method TestClass testClass = new TestClass(); testClass.myMethod(5); } //Defining the function public static void myFunction(int num) { System.out.println("Hey, I stand alone. I am stronger"); System.out.println("I have " + num + " points"); } } class TestClass{ public void myMethod(int num){ System.out.println("Hey, I always have my family with me. I am stronger"); System.out.println("I have " + num + " points"); } }
So, as you can see, a method exists only if there is a class. We can only call methods once we create an object of those classes.
Parameters vs Arguements
Let me solve this little debate too. I have heard so many beginners use these two words interchangeably.
A parameter is a variable in the method/function definition. In the code snippet, num is a parameter.
Arguments are the values that we pass into those methods/functions. In the code snippet, 5 is an argument.
Now I hope you understand why I specifically used the word functions in the description.
Local vs Global Variables
public class Test { //Declaring the global variable public static final int NUM_OF_ROUNDS = 10; public static void main(String[] args) { //Declaring the local variable int currentRound = 1; //Iterating 10 rounds for (currentRound = 1; currentRound <= NUM_OF_ROUNDS ; currentRound++) { System.out.println("This is round number " + currentRound); } } }
If you still didn’t understand by reading the code snippet, a local variable is defined inside a function and it is only valid inside that scope.
A global variable on the other hand is defined outside of a function (inside the class) and can be used inside the scope of the class.
Can we define two variables with the same name?
Yes. We can do that as long as those variables are not in the same scope.
Variable types
1. Instance Variables
When a variable which is declared inside a class and outside all the methods and blocks, we call it as an instance variable. The scope of instance variables is throughout the class except for static methods.
2. Class Variables
When a variable is declared inside a class, outside all the blocks and is marked static, we call it a class variable. The scope of class variables is the scope of the class itself.
3. Local Variables
All other variables that are not either class or instance variables are called local variables. The scope of local variables is the block in which it is declared.
This is the end of the second article of this article series. I hope you guys learned something valuable today. Thank you for taking the time to read the article. If you liked it, make sure you hit the subscribe button and share the article among your friends. Stay safe ✌