Functions and why use them

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.

Example of function


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


Emil 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 data type of the return value

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.

  • Function Parameters
  • Return type
  • Statements to be executed

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()

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())

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)

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))

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.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store