Functions and why use them

Website Developer
9 min readDec 4, 2021


Functions are a set of instructions bundled together to achieve a specific outcome. Functions are an excellent alternative to having repeating blocks of code in a program. Functions also increase the reusability of code. Values can be passed to a function using variables — we call these parameters or arguments. Functions can also return values.

In computer programming, a function is a named section of a code that performs a specific task. This typically involves taking some input, manipulating the input, and returning an output. In this post, we will go over how to define Python functions, specify functions that can take an arbitrary number of positional arguments, and specify functions that can take an arbitrary number of keyword arguments. A function is a block of code that only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result.

History of functional programming

The lambda calculus, developed in the 1930s by Alonzo Church, is a formal system of computation built from function application. In 1937 Alan Turing proved that the lambda calculus and Turing machines are equivalent models of computation, showing that the lambda calculus is Turing complete. Lambda calculus forms the basis of all functional programming languages. An equivalent theoretical formulation, combinatory logic, was developed by Haskell Curry in the 1920s and 1930s. Church later developed a weaker system, the simply-typed lambda calculus, which extended the lambda calculus by assigning a type to all terms. This forms the basis for statically-typed functional programming.

The first functional programming language, LISP, was developed in the late 1950s for the IBM 700/7000 series of scientific computers by John McCarthy while at the Massachusetts Institute of Technology (MIT). LISP functions were defined using Church’s lambda notation, extended with a label construct to allow recursive functions. Lisp first introduced many paradigmatic features of functional programming, though early Lisps were multi-paradigm languages and incorporated support for numerous programming styles as new paradigms evolved. Later dialects, such as Scheme and Clojure, and offshoots like Dylan and Julia, sought to simplify and rationalize Lisp around a cleanly functional core. At the same time, Common Lisp was designed to preserve and update the paradigmatic features of the numerous older dialects it replaced.

Information Processing Language (IPL), 1956, is sometimes cited as the first computer-based functional programming language. It is an assembly-style language for manipulating lists of symbols. It does have a notion of the generator, which amounts to a function that accepts a function as an argument. Since it is an assembly-level language, code can be data, so IPL can be regarded as having higher-order functions. However, it relies heavily on the mutating list structure and similar imperative features.

John Backus presented FP in his 1977 Turing Award lecture “Can Programming Be Liberated From the von Neumann Style? A Functional Style and Its Algebra of Programs”. He defines functional programs as being built up in a hierarchical way utilizing “combining forms” that allow an “algebra of programs”; in modern language, this means that functional programs follow the principle of compositionality. Backus’s paper popularized research into functional programming, emphasizing function-level programming rather than the lambda-calculus style now associated with functional programming.

Example of function


def my_function(fname):  print(fname + " Refsnes")my_function("Emil")my_function("Tobias")my_function("Linus")


Emil Refsnes

Tobias Refsnes

Linus Refsnes

Advantages using of functions

  • It helps to divide the large programs into small groups to read the code and debug it faster and better.
  • Python Functions stop us from writing the same logic various times. We can bind the logic in one function and then call the same repeatedly.
  • Many persons can work on the same program by assigning different functions to each.
  • It encourages us to call the same function with different inputs multiple times.
  • The use of functions enhances the readability of a program. A significant code is always tricky to read. Breaking the code into smaller Functions keeps the program organized, easy to understand.
  • It reduces the complexity of a program and gives it a modular structure.
  • If we need to test only a particular part of the program, we will have to run the whole program and figure out the errors, which can be quite a complex process.
  • Functions can be shared and used by other programmers.
  • Reducing duplication of code.
  • Improving the clarity of the code.
  • Information hiding

Disadvantages of using functions

  • Immutable values combined with recursion might lead to a reduction in performance.
  • In some cases, writing pure functions causes a reduction in the readability of the code.
  • Though writing pure functions is easy, combining the same with the rest of the application and the I/O operations is tough.
  • Writing programs in recursive style using loops for the same can be a daunting task.
  • Writing pure functions are easy, but combining them into a complete application is where things get complicated.
  • The advanced math terminology (monad, monoid, functor, etc.) makes FP intimidating.
  • For many people, recursion doesn’t feel natural.
  • Because you can’t mutate existing data, you instead use a pattern that I call “Update as you copy.”
  • Using only immutable values and recursion can lead to performance problems, including RAM use and speed.
  • Since there’s no state and no update of variables is allowed, performance loss will occur. The problem arises when we deal with a large data structure, and it needs to perform a duplication of any data even though it only changes a small part of the data.
  • Compared to imperative programming, much garbage will be generated in functional programming due to the concept of immutability, which needs more variables to handle specific assignments. Because we cannot control the garbage collection, the performance will decrease.

Different types of function in Python

Built-in functions:

such as help () to ask for help, min () to get the minimum value, print () to print an object to the terminal. A function that is already defined in a program or programming framework with a set of statements performs a task and is called a Built-in function. So, users need not create this type of function and can use it directly in their program or application. Many builds in functions are available in most of the programs or programming frameworks, and these differ from each other; each one has its unique functionality. Any function provided as part of a high-level language can be executed by a simple reference with a specification of arguments.

The critical contribution of the system-supplied functions is that they provide clarity to the program because they do not have to be redefined. They are entirely debugged, efficient, and always produce a precise output. This saves the programmer’s time from writing own functions. It reduces the source code, which saves time for the programmer. When using this type of function, the following should be noted. A system–supplied process involves two data types.

  • The data type of actual parameter
  • The data type of the return value

The above data types may or may not be the same. Some functions accept and return the same data

Few build-in C functions are: — strcpy(), strcmp(), strcat(), malloc(), caaloc(), free(), realloc().

Few build-in Java functions are: — toFixed(), toString(), valueOf(), charAt(), concat().

Few build-in python functions are:bool (), bytearray (), dict (),float (),format (), max ()

Few build-in c++ functions are: — exit(), sqrt(), pow(), strlen()

User-Defined Functions (UDFs):

which are functions that user create to help them out. The functions that we create in a program are known as user defined functions or in other words you can say that a function created by user is known as user defined function.

Any function has four building blocks to be declared –

  • Function name
  • Function Parameters
  • Return type
  • Statements to be executed

A function is a block of code that performs a specific task. It allows you to define functions according to your need. These functions are known as user-defined functions. User-defined functions allow programmers to create their routines and procedures that the computer can follow; it is the basic building block of any program and also very important for modularity and code reuse since a programmer could create a user-defined function that does a specific process and calls it every time it is needed. Their syntax depends entirely on the programming language or application created. For example:

Example of a user-defined function in Python


def compute_square(num_to_square):    return num_to_square * num_to_squarenum_squared = compute_square(7)print('7 squared is', num_squared)


7 squared is 49

Types of user-defined functions

Function with no arguments and no return value:

Example of code

# Python Function with No Arguments, and No Return Valuedef Adding():    a = 20    b = 30    Sum = a + b    print("After Calling the Function:", Sum)Adding()


After Calling the Function: 50

Function with no arguments and a return value:

Example of code

# Python Function with No Arguments, and with Return Valuedef Multiplication():    a = 10    b = 25    Multi = a * b    return Multiprint("multiplication is : ", Multiplication())


multiplication is: 250

Function with arguments and no return value:

Example of code

# Python Function with Arguments, and NO Return Valuedef Multiplications(a, b):    Multi = a * b    print("After Calling the Function:", Multi)Multiplications(10, 20)


After Calling the Function: 200

Function with arguments and a return value:

Example of code

# Python Function with Arguments, and  Return Valuedef Addition(a, b):    Sum = a + b    return Sum# We are calling the Function Outside the Function Definitionprint("After Calling the Function:", Addition(25, 45))


After Calling the Function: 70

Why do we use user-defined functions?

- They allow modular programming.

You can create the function once, store it in the database, and call it any number of times in your program. User-defined functions can be modified independently of the program source code.

- They allow faster execution.

Similar to stored procedures, Transact-SQL user-defined functions reduce the compilation cost of Transact-SQL code by caching the plans and reusing them for repeated executions. This means the user-defined function does not need to be reparsed and reoptimized with each use resulting in much faster execution times. CLR functions offer a significant performance advantage over Transact-SQL functions for computational tasks, string manipulation, and business logic. Transact-SQL functions are better suited for data-access intensive logic.

- They can reduce network traffic.

An operation that filters data based on some complex constraint that cannot be expressed in a single scalar expression can be defined as a function. The function can then be invoked in the WHERE clause to reduce the number of rows sent to the client.

- Reduction in Program Size:

Since any sequence of statements that are repeatedly used in a program can be combined to form a user-defined- function. And these functions can be called as many times as required. This avoids the writing of duplicate code, again and again, reducing program size.

- Reducing Complexity of Program:

Complex programs can be decomposed into small sub-programs or user-defined -functions.

- Easy to Debug and Maintain:

During debugging, it is very easy to locate and isolate faulty functions. It is also easy to maintain a program that uses user-defined functions.

- Readability of Program:

While using a user-defined function, a complex problem is divided into different sub-programs with clear objectives and interfaces, making it easy to understand the logic behind the program.

- Code Reusability:

Once a user-defined function is implemented, it can be called or used as many times as required, reducing code repeatability and increasing code reusability.

Is functional programming the future’s best coding paradigm

Functional programming has been in existence for the last six decades, but so far, it hasn’t ceased to overcome the general use of object-oriented programming. With the explosive growth of machine learning and big data, functional programming has been growing in popularity because of the simplicity of paralleling pure functions. Code for data analysis tasks and workflows is easier to follow, test, and maintain using the functional paradigm, lending to its growing use in the future. Functional programming has been around for the last 60 years, but it’s always been a niche phenomenon so far. Although game-changers like Google rely on its key concepts, the average programmer of today knows little to nothing about it. That’s about to change. Languages like Java or Python are adopting more and more concepts from functional programming. Newer languages like Haskell are going completely functional.

Take away

Without functions, programs would have loads of duplicate code, wouldn’t flow logically, and would have no separation of utility. That would be a nightmare for managing, testing, and debugging. Thank goodness programming languages use them! Now you know why programming languages use functions and why they’re so important. The biggest reasons for including functions are one truth: functions allow you to break a program into more manageable pieces. Your program becomes simpler to manage, easier to test, and apt for reuse when you do this. Functional programming can be considered an elementary part of today’s IT landscape.