Codementor Events

Functions in Python

Published May 02, 2022Last updated May 04, 2022
Functions in Python

In This Article We are going to learn how functions will work.

Let's first discuss What exactly is a Python Function.
In Python, Function is a group of related statements that performs a specific task.

With the help of functions we can break our Program into small group of repetative chunks. So, when our program grows larger and larger, functions helps us to make more organized and Managable. So that we can avoid repetation and makes our code reusable.

In Python we are having two types of functions:
1. Built-in Functions: These functions are Pre-defined functions in Python
2. User-Defined Functions: These functions are defined by the user to perform a specific task.

Syntax:

def fun_name(parameters):
    fun-block
return expression

Now we will try to understand the syntax.

  1. To define function we use def keyword.
  2. A function can have it's parameters, but these are optionals.
  3. The function block is started with the colon[:] symbol.
  4. we need to follow the indentation for function block statements.
  5. To return the value from the function we need to give return statement. A function can have only one return statement.

python-functions.png

We can define functions with parameters and without parameters.

Defining A function Without Parameters:

In this we can call the function by typing its name followed by paranthethis(). When we call them it will print hello sadhana.
Let's see function without parameters example:

def say_hello():
    print("Hello")
    print("sadhana")
say_hello()

Defining A Function With Parameters:

In this we can give parameters. the parameters are used when we want to pass data into our function. When we call a function we need to pass arguments to the function.


def evenorodd(number):
    if(number%2==0):
        print("{} even".format(number))
    else:
        print("{} odd".format(number))
evenorodd(20)

Multiple Parameters:

In functions we can pass multiple parameters also. We just need to seperate them with commas

def add(a,b):
    return a+b
add(2,3)

In above example the a wiil take 2 as value and b will take 3 as a value.
The output will be 5.

The Return statement:

In a function we use the return statement at the end of the function and it helps us to return the result of the function.
We can't use return outside of the function.

return expression

function-return.png

it contains the expression which gets evaluated and the value is returned to the caller function. If the return statement has no expression it returns the None object.
let us see some examples

Creating Function With Return statement:

def sum():
    a = 10
    b = 20
    c = a + b
    return c
print("The sum is:", sum())

In this above code we have defined the function sum and it has a statement c = a+b, which computes the given values, and the result is returned by the return statement to the caller function.

Creating function without return statements:

def sum():
    a = 20
    b = 40
    c = a+b
print(sum())

In this above code we defined the same function but without return statement, The function will return None as output.

Arguments in Functions:

Arguments are types of information that passed to functions. We can pass any number of arguments to the functions, but we have to seperate them with comma.

let's see some examples.

def func_name (name):
    print("Hi",name)
func_name("Sadhana")

Output
Hi Sadhana
def sum (num1,num2):
    return num1+num2;
# Taking values from the user as an input
num1 = int(input("Enter the value of num1: "))
num2 = int(input("Enter the value of num2: "))
# Calculating and Printing the sum of num1 and num2
print("Sum = ",sum(num1,num2))
OutPut:

Enter the value of num1: 5
Enter the value of num2: 13
Sum =  18

Hope You Have Loved Reading About Implementation of Functions at Basic Level. Keep Practicing To Get Grip on The Topic.

Discover and read more posts from Sadhana Reddy
get started
post comments2Replies
Kumar Raja Gattamaneni
3 years ago

Hi, Nice attempt on fundamentals. Please watch out for indentation problems in the code snippets you are sharing. Secondly you can extend this with a quick touch on variable number of arguments or it can be your topic for another article too :)

Sadhana Reddy
3 years ago

Thank you very much for your feedback. I will definitely work on it.