Understanding Functions In Programming

Understanding Functions In Programming

Table of Contents


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


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:


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:


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”.

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

Leave a Reply