Functions in Python: A Beginner’s guide to know about it.

Tejas Magade
4 min readDec 2, 2020
Function in Python

We all know how big is the function plays role in each programming language similarly, in python there is also a concept called function.

Defn of function

A function is a group of statements that are used to perform certain tasks.

Python already has some built-in functions present, some of which are as follows.
1. print() — As we know the print() function in Python, we use it to show the output.
2. sqrt() — Using this function, we can directly extract the square root of a value.
3. power() — Using this function, we can extract the power of a value.

These and many more built-in functions are available in Python language.

Advantages of functions

1. Once written, we can use the code whenever we want. Therefore, functions are also called reusable code.
2. Code redundancy functions are avoided.
3. Modular programming makes programming easier.
4. The code makes the function easier to handle.
5. The use of Function reduces the length of the program.

So, now let us see how to create a function and how to use it in python. For that, we are doing two steps one is Defining a function and the other Calling a function.

Defining a Function

But whenever you want to use a function, first you have to tell the compiler what the function is going to do. For that, first, we define the function.

In Python, we use the keyword def to define a function. And then we add the name and parenthesis () of our function. In Parenthesis () the parameters of your function are passed.

You will notice by looking at the following syntax.

Let us now look at a simple program.

Addition of Two numbers in Python program

on the above code, we are creating a function to add two values.

def sum (a, b): Using the keyword def, we are telling the compiler that this is my function and I am defining it. Next to def is written sum() which denotes that the name of the function is a sum. And in the subsequent parenthesis (), two variables are passed named a and b. After parenthesis, we have used a colon : which means that the body of the function starts from here.

Then we start writing the main body of the function. before writing the next code we have to follow the indentation rule of python (Indentation refers to the spaces at the beginning of a code line). So, we write the expression c = a + b in the body for storing the addition of two values in variable c and then we print the value which is present in c by using print(c).

Now we have told the compiler what is going to happen in the function but we still want to tell the compiler where we want to call that function and we will follow the next process.

Calling a Function

If you define a function, it will never run automatically, so you have to call the function. And for that, we have to type the code in this syntax function_name (parameter1, parameter2, ….). Now as we were looking at the program of the addition of two numbers we have defined the function in the above block and now we will see how we will call that function. For that, we have to use the syntax sum(5, 7).

The first name of the function and parenthesis next to it is very important for function calls. For this program of “addition of two numbers,” we are using this code sum(5, 7) for calling a function. And what value will go in that parenthesis depends on your function. If your function accepts some parameters then you have to pass the parameters in that parenthesis & if you don’t want any values ​​in the function then it will work even if you don’t pass any parameter in it. It all depends on your function what type of your function is?.

So, this is all about Functions in Python.

Below here are some basic programs that will help you to understand more about the function.

1. Check whether a number is Prime or not

Check whether a number is Prime or not

2. Convert temperatures to and from celsius, fahrenheit

Convert temperatures to and from celsius, fahrenheit

3. Print the Fibonacci sequence

Print the Fibonacci sequence

--

--

Tejas Magade
0 Followers

Persuing a degree in I.T Engineering, A Programmer, & Blog Writer. I love to do Code.