DEV Community

loading...

Introduction to Python Functions

Namaiyian
・4 min read

PYTHON PROGRAMMING
In the world of computer programming, we define a function as a named block of code that performs a specific operation on some specified inputs and produces a result. Today, we will be taking a look at functions in Python. We will learn how to define functions, how to call functions and a few other concepts related to functions. The aim is for each of us to have a basic understanding of functions as we start to implement them in our programs.

Importance of Functions in Python Programming

  • Functions contribute in making our code more organized. Writing all our code in the main function makes it very cumbersome and difficult to work with. By creating several functions in our program apart from the main function, we are able to break down our task into small manageable sections and work more efficiently.
  • Functions allow code reusability. In programming, there are cases where we need to perform a task multiple times in the same program. Copying the same lines of code again and again would be very tedious. By using functions, we only write the code once and call the function when needed. This reduces the need for the programmer to repeat himself or herself.
  • Functions allow sharaebility of code. Functions are self-contained, hence they are quite portable. Commonly, the functions written by one programmer can be used by another programmer in another project. This enables better use of programming resources and cuts down on duplication.
  • Functions allow easier testing and debugging of code. When a program is written with clear functions, there is less code to test. It also makes it a whole lot easier to pin point the issue when debugging.
  • Functions make it easier to extend our program. When there is need to add new functionality to our program, it is easier to use functions. This is because functions allow us to make one change that can take effect on the whole program when the function is called.

TYPES OF FUNCTIONS IN PYTHON

In Python, there are several types of functions. The differ in a number of ways, including the method of definition and the general structure. They include the following:

I) Built-in Functions

These are functions that have already been pre-defined in Python. Their functionality is known and they can be called at any point in the program. Therefore, we do not need to define them in any way. Examples include print(), len(), append() among many others. Below is an example of how we use the print() function:

name = "Grace"
print(name)
Enter fullscreen mode Exit fullscreen mode

The output of the code above will be:

Grace
Enter fullscreen mode Exit fullscreen mode

II) User-Defined Functions

These are functions that are defined by the programmer. He or she determines the name of the function, its inputs, its functionalities and its outputs.
They are defined using the def keyword. The general structure of a user-defined function is as follows:

def function_name(parameters):
    code
    code
    code
function_name(arguments)
Enter fullscreen mode Exit fullscreen mode

From above, function_name stands for the name of the function. It should be as descriptive as possible. Parameters are names of the inputs that we would like to use in our functions. They are normally enclosed by parantheses. Next, we write the block of code that defines the operations that are to be performed on the inputs. Finally, we call the function. This means that we are instructing the computer to perform the specified operations on the set of inputs that we are going to provide. We do this by writing the function name with the arguments(the actual values represented by the parameters) in parantheses.
Let us now look at an actual example of a function and analyze its output.

def add(a, b):
    return(a + b)
add(5, 10)
Enter fullscreen mode Exit fullscreen mode

Output:

15
Enter fullscreen mode Exit fullscreen mode

The code above defines a function add that takes two parameters, a and b.The function has a return statement, which says that the output of the function should be the sum of the values of a and b. The function has then been called, with the arguments 5 and 10.
As we can see, the output is 15.

In Python, we can also define functions which have no return statement. They are referred to as void functions. The following example demonstrates how they work:

def speech(greeting):
    print(greeting)
speech("Hey friends!")
Enter fullscreen mode Exit fullscreen mode

Output:

Hey friends!
Enter fullscreen mode Exit fullscreen mode

Furthermore, we can define functions that have no parameters, meaning they require zero inputs. For example:

def song():
    print("Twinkle twinkle little star!")
song()
Enter fullscreen mode Exit fullscreen mode

Output:

Twinkle twinkle little star!
Enter fullscreen mode Exit fullscreen mode

Calling the function song does not require us to pass any arguments. It will always give the same output.

III) Anonymous Functions

Grammatically, anonymous means not defined by name. Therefore, anonymous functions are those functions that are defined without a name. They are also referred to as lambda functions. Anonymous functions are normally defined on one line.
Lambda functions are quite similar to user-defined functions, since they allow the programmer to define their functionality. With that said, would somebody be right to classify a lambda function as a form of user-defined function? Feel free to share your thoughts down below.
Anonymous functions are defined using the lambda keyword. The syntax is as follows:

lambda parameters : expression
Enter fullscreen mode Exit fullscreen mode

Let us take a look at a working example:

square = lambda n : n ** 2
print(square(4))
Enter fullscreen mode Exit fullscreen mode

Output:

16
Enter fullscreen mode Exit fullscreen mode

The above function takes one argument n, and computes its square. One use of lambda functions is that they are used as arguments for higher order functions(functions that take other functions as arguments).

In conclusion of today's post, I would like to encourage every beginner and intermediate programmer to fully utilize functions in Python. You can be assured that mastering the use and manipulation of functions will make your coding experience ten times better. Happy coding!

Discussion (0)