Hello friends! Today I will talk about methods, one of the fundamental building blocks of programming. You know those small pieces of code we write to avoid doing the same task repeatedly? Yes, those. When I first started, I saw methods just as a ‘shortcut,’ but over time I realized they are actually the lifeblood of programs. Isn’t that wonderful?
Methods can be thought of as functions. In other words, they are code blocks that perform a specific task, contain a sequence of commands, and execute those commands when called. Imagine you’re cooking a meal, and the recipe says ‘wash and chop the vegetables.’ That part, ‘wash and chop vegetables,’ is like a method. Instead of repeating this process every time you use a vegetable, you define it once and call it whenever needed.
By the way, when we talk about methods, parameters and return statements immediately come to mind. Parameters allow us to send information from outside to methods. Like ‘2 tomatoes’ in a recipe. Tomatoes, in this recipe, become a parameter. Thanks to this parameter, our method knows how many tomatoes to use.
Parameters make our methods more flexible. We can perform the same operation with different inputs. For example, think of a method that adds numbers. If it doesn’t take parameters, it will always add the same two numbers, but if it accepts parameters, you can add any two numbers you want. Isn’t that great!
Now, let’s talk about the return statement. This allows methods to send us a value back. Like the ‘washed and chopped vegetables’ result in a recipe once the task is done. In programming, this can be a calculated value, a result, or some information about a state. Just like pressing the ‘+’ button on a calculator shows the result. Without a return statement, a method would perform its task but wouldn’t communicate the result, as if it were speaking to itself.
This return event can sometimes be confusing because some methods do not return a value. For instance, a method that writes something on the screen may not return a value. These are often called ‘void’ methods. Think of it as ’empty.’ But their work is still important. For example, a method displaying a message box shows something on the screen even if it does not return anything.
Let’s solidify this with a code example. For example, a simple C# program that takes two numbers from the user, adds them, and displays the result, showing both parameter usage and return statement.
First, let’s see a wrong example and identify where mistakes can happen. Then, we’ll discuss the correct version.
// WRONG EXAMPLE: A method that adds numbers without returning a value and takes input directly inside the method
public void AddAndDisplay() { int num1 = 5; int num2 = 10; int result = num1 + num2; Console.WriteLine("Sum: " + result); // Outputs to screen but we cannot use the result elsewhere. }
As you see, this method directly prints the result to the screen. You call it like: AddAndDisplay();. But you cannot use the sum in other calculations or assign it to another variable because it does not return a value. It just performs its task and ends. To use this method properly, you’d need to do something like: AddAndDisplay();
Now, let’s look at the proper way. We’ll write a method that takes parameters and returns the calculated sum, so we can use the result wherever we want.
// CORRECT EXAMPLE: Method that takes parameters and returns the sum value
public int AddAndReturn(int num1, int num2) { int result = num1 + num2; return result; // Sending the calculated result outward. }
It’s that simple! When you call ‘AddAndReturn’, you pass two integer parameters. The method adds these numbers and returns the result as an ‘int’ type. How will we use this? For example:
int myResult = AddAndReturn(20, 30);
Console.WriteLine("My result: " + myResult);
This code first calls ‘AddAndReturn’ with 20 and 30. The method returns the sum 50, which is assigned to ‘myResult.’ Then, it is printed on the screen. Isn’t that wonderful? Now we can use the result of the addition as we wish.
In short, methods, parameters, and return statements form the core of programming. Understanding these will make your code more organized and more understandable. If you want to learn more in detail, you can check Microsoft’s official documentation. They offer great examples, like searching with: Microsoft documentation.
In some languages, methods behave exactly like functions. Python, for example, defines functions a bit differently but with the same logic—packaging a task and invoking it later.
Remember, one of the most important principles in coding is avoiding repetitive code. Methods are perfect for that. Write once, use as many times as you want. This saves time and increases code readability. It might seem unnecessary to write methods for simple tasks, but as you develop good habits, they will significantly ease your work in more complex projects. Believe me!
Ultimately, methods are indispensable in programming. They accept information through parameters and give us results via return. When you understand this cycle, you will have taken a major step forward in your coding journey.