Table of contents
Have you ever sent a document to a printer without carefully checking the content of the document? ๐ Well, it happens to the best of us. Sometimes this results in overprinting, at other times, a reprint is necessary. It's weird how we hardly blame the machine unless something jams or it's really slow. We know that printers are built to do one thing and one thing alone, accept documents and print them, that clarity of purpose is why they're so reliable in a pinch.
This brings me to the topic of functions. Recall that I mentioned functions in passing yesterday when I wrote about events. Functions are blocks of code that are organized in such a way that they have one responsibility and will execute when called upon. Functions are one of the most integral parts of software development. There's even a whole paradigm of software development known as functional programming, and it's hugely efficient due to how decentralized blocks of code are.
Functional programming makes it easy to reuse code and helps us (software engineers) ensure our code abides by DRY (Do not Repeat Yourself) principles. Technically, we software engineers hate having to do the same thing twice (which is why we're known for automation). Functions are used to save time and repetition by creating dedicated blocks of code that can be called by other variables, methods, and classes in order to deliver consistent results relative to the values being sent to the function and even in the absence of values.
If we categorize them by parameters, there are basically two types of functions: functions that accept parameters and functions that don't. If we categorize them by return type, there are those that don't return anything and those that return something.
A relatable explanation
We typically specify the data type of the value being returned (especially in strongly typed languages). To put it in simpler terms, there are functions that act like printers; they take input values (your document) and return output to you (the printed document), while some functions act like dispatch riders in the sense that you give them input values (your package) and they use them to run an errand but don't return anything to you.
There are also functions that act as a pumping machine, you turn it on (by calling the method), and it runs, carrying out preset responsibilities (pumping fluid/liquid), doesn't take any value from you (except being called to action), doesn't return anything to you at the end of the day (recall that the water is in the tank, not returned directly to you), but executes the call to action.
A code snippet
A simple example could be a function that returns my full name as one value. The screenshot below will give you an idea as to how developers make functions, the code snippet below should show you how
public static string firstName = "John";
public static string lastName = "Doe";
// The first two variables are used to obtain the first name and the last name
// We're using the fullName variable to call the GetFullName function.
string fullName = GetFullName(firstName, lastName);
// The GetFullName function below is what is being called by the variable above.
//The function has a data type of string (which basically means a word) and at the end it returns a word.
public static string GetFullName(string value1, string value2) {
string fullName = $"{value1} {value2}";
return fullName;
}
Finally
Function are one of the building blocks of modern software development, it does make things easier, cleaner, and helps increase the modularity of code. For newbies, it will be the basic way you know how to pass data around in a software solution.