07Jan 2021

Understanding Functions In Programming

Introduction

When we write programs, design software, and build websites or applications, we can’t avoid the fact that we will have a need to make use of functions.

Because as much as we think these are technical terms, it’s important to understand that this is a basic of computer programming, and knowing how to, where to, and when to use functions is as important as knowing the definition of programming.

In this article, I will be explaining functions in all its sense. I hope that after reading this article, you can understand what a function is, you can explain the term to a newbie or a curious mind, you understand the importance of using functions and can create and make use of one in your projects.

This article is tailored to someone or anyone that wishes to better understand what functions in programming are.

In the article, we will be writing our functions in dart programming language, but I’ll advise you to read anyways, the major thing is “understanding functions in programming”.

What is a Function

what-is-a-function

A function can be defined as a named pack of organized and reusable code that performs a specific task or a related task. In programming, functions are very important for a couple of reasons, but most importantly, reusability is the first factor.

In the simplest form of understanding, functions in programming are there to give functionality. When we use functions in our programs we’re trying to package pieces of code together into a block, this would allow us to call the block/function whenever we need the piece of code to do something.

These blocks of code save us from typing the same lines of code and calling them in our program every time we need them to do something. A function can optionally take in parameters and return values.

Hence, a function can either be a library function or a user-defined function. A library function also referred to as the built-in function is a pre-written set of tasks/ functions created by individual programming languages and stored in libraries.

While on the other hand, a user-defined function is a set of functions written by the user to perform some specific tasks.

Defining a Function in Dart

When we discuss functions in any programming language, it is the same concept, maybe a slight difference in how the language is structured/syntax, but it’s majorly the same way that all functions work.

Dart is an object-oriented language, and that means functions can be assigned to variables or passed as arguments to other functions.

Therefore, if you’re coming from a different language, there is no need to panic, it’s all the same. Let’s take a look at a basic dart function:

defining-a-function-in-dart

In declaring functions in the dart programming language, there are a couple of ways to do so, firstly we have the arrow functions.

The arrow function is a shorthand syntax for functions that has just one expression(An expression is any valid unit of code that resolves to a value):

>> String makeGreeting(String name) => 'Hello, $name';

The arrow function implicitly returns the result of the expression, which is exactly the same as:

String makeGreeting (String name) {
‘Hello $name’;
}

So, in simpler terms, we use the arrow function in cases where the function has just one line of code. This doesn’t mean you’re forbidden from defining functions the traditional way, but ain’t just one line better?

To avoid a compile time error, it is important that our function is defined, else the main function will be unaware of our function.

A more detailed structure for functions in dart is:

detailed-structure-for-functions-in-dart

The above structure is a general syntax of a dart function with parameters and return value where:

name_of_function is replaced with the name we wish to give our function or group of tasks.

Parameters according to wiki “is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine.

It can be positional parameters, named parameters, and optional positional and named parameters, or a combination of all of them.

Return values are simply the values that a function returns when it has completed. It is important to note that the datatype of a value returned must match the return type of a function. Whereas, the void keyword can be used to imply no return value.

When creating a function, we give the function the definition of what the function has to do. And to use the function, you have to call that function to perform a defined task.

The act of using a function in programming is referred to as “Calling the function”.

A Short Function Story

And for people that still would love to understand the concept of functions, I’ll explain with an analogy.

We are 2 friends sharing an apartment. And let’s say we’re really busy developers and the assistance from an extra hand would do us some good. But because we are so busy working and covering all the chores in the house, we decide to build ourselves a robot.

The job of this robot will be to help us do some chores that are repeated daily while we focus on work and other fun things. We built one or let’s say an MI friend of ours helped us build one and we named the robot ‘dartan’.

However, on the first day, while we were working, we needed some fresh smoothie and called our dear dartan to come make us some. We punch in the commands as follows:

  1. Get the fruit bowl from the fridge.
  2. Get some ice too.
  3. Rinse the blender
  4. Pick up the orange, banana, and pineapple and pour them into the blender.
  5. Turn on the blender and let it grind for 20 seconds.
  6. Turn it off, wait for 10 seconds and grind again.
  7.  Pour into a glass cup and serve for two.

Ok, I agree I may be the worst smoothie maker you’ve ever met, but I just need us to focus on the concept and not the juice, please.

Dartan does this for the first day, and it works just fine. On the second day, we key in the commands again and then the third day and a day after. At the end of the first week, we could’ve possibly gotten bored of telling Dartan what to do.

And worst we may just consider doing it ourselves. So, what’s our best solution?

Yes, you’re right! We have to package these many lines into a block of code. In lot’s of programming languages, we will notice that blocks of code are usually enclosed in curly brackets({}).

And to organize our instructions, we just need to declare our function like this:

Function makeJuice() {

  • Get the fruit bowl from the fridge.
  • Get some ice too.
  • Rinse the blender
  • Pick up the orange, banana, and pineapple and pour into the blender.
  • Turn on the blender and let it grind for 20 seconds.
  • Turn it off, wait for 10 seconds and grind again.
  •  Pour into a glass cup and serve for two. ‘
}

After declaring the recipe in a function, all we have to do is tell Dartan to makeSmoothie(). And dartan knows what to do, because it will look for the function ‘makeSmoothie()’, execute it and return with a cool glass of smoothie.

Furthermore, this function will execute the commands from the top to the bottom and in an orderly sequence.

And if for example, there are conditions like changing fruits and adding honey or coconut, we could just include it by just adding it as an option/parameter to the function and updating it. This in turn makes our function more adaptable.

This way, we don’t have to always type in lines of code from scratch to carry out the same task. We’ve not only saved ourselves some time, but we’ve also managed to utilize the important functions.

Functions in programming are very useful because when we call a function, it executes, and returns a value.

Why Should I use a Function?

why-should-i-use-a-function

I would love to define functions as the bread and butter of reusable codes because they let us define the vocabulary we would like to use in our programs.

Functions are as important as every single concept in programming and have these reasons to thank.

When we use functions in our programs;

  • It promotes and encourages re-usability.
  • It encourages the modular-approach in solving problems
  • It promotes teamwork.
  • It reduces duplication thereby reducing development time.
  • It optimizes the code.
  • It reduces decoupling.
  • It promotes healthy and clean code practices, generally.

When do I need to use a Function?

when-do-i-need-to-use-a-function

In developing solutions, we can always have a need to either make our codes simpler, easier to read, reusable, or just easier to manage.

In the quest to meet all these requirements we need to understand how to and when to use functions.

I mean, if you ask me, I’ll say every time. Instead of writing your name a 100 times, just write a few lines of code, put it in a block, and call the function when you need to do something.

Conclusion

Hi there!

If you’re here now, you probably finished reading already, or you just skipped to the end to see what the ending says.

Either way, I have done my bid to explain the concept of functions not just as a dart programmer or a newbie developer or a senior developer, but as someone that struggled to understand these technical jargons and found a way around it.

In summary, a function is used to segregate the different functionalities of a program and oftentimes combined to perform tasks.

This, in turn, reduces repetitiveness thereby resulting in ease-of-use, fewer lines of code, and more efficient maintenance.

However, I’ve written this article to help you understand what functions are and why we need to use them when writing codes.

In our journey through solving problems using programming, we will always have ways to improve our efficiency while providing solutions.

Thank you for reading!

Acodez is a renowned web development company and web application development company in India. We offer all kinds of web design and Mobile app development services to our clients using the latest technologies. We are also a leading digital marketing company providing SEO, SMM, SEM, Inbound marketing services, etc at affordable prices. For further information, please contact us.

Looking for a good team
for your next project?

Contact us and we'll give you a preliminary free consultation
on the web & mobile strategy that'd suit your needs best.

Contact Us Now!
Jamsheer K

Jamsheer K

Jamsheer K, is the Tech Lead at Acodez. With his rich and hands-on experience in various technologies, his writing normally comes from his research and experience in mobile & web application development niche.

Get a free quote!

Brief us your requirements & let's connect

Leave a Comment

Your email address will not be published. Required fields are marked *