Functions in Python

Functions in Python

Python Functions are the block of code that will return particular data. Functions only run when we call them. We can call the function many times to run the same block of code instead of writing the code itself.

Syntax of Python Functions:

def function_name(parameters):
    block of code

    return data


In the syntax, the terms used are:

  • def - Python keyword used to define the function

  • function_name - User-defined name of the function

  • parameters - We can only use the parameters as variables in the scope of the function.

  • block of code - They are statements that run when we call the function.

  • return - Returns values while calling the function so that we can use the function as a variable.

Example (ordinary Python functions):

def func_name(name):

your_name = "Ankur"

Here, the func_name() function has one parameter name. The function only prints the passed argument.

Parameters and Arguments

Data can be passed into the function while calling it. While defining a function, parameters can be specified. The syntax is:

def my_function(parameters):
    return expression


Example (parameter and arguments):

def eligible_to_vote(age):
    if(age > 18 or age == 18):
        return True
        return False

your_age = 17

The above function will take the integer "age" as an argument. It returns True if the integer "age" is greater or equal to 18. But, returns False in any other case.

1. Default Arguments:

Default arguments are the parameters whose value is pre-declared when the function is defined. They are optional since the interpreter won't throw an error if we don't pass them while we call a function.


def function_name(parameter=value):
    return expression

Here, the value is pre-defined but if we pass an argument while calling the function, we can use a custom value for that parameter.

Example (default argument):

def power(num, exp=2):
    return num ** exp


The above program will return the square of 2. However, if we want the exponent to be 4, we can write the following code.

def power(num, exp=2):
    return num ** exp

print(power(2, 4))

2. Keyword Arguments

We can use keyword arguments if we don't want to mess with the order of the parameters. The syntax is:

def my_func(parameter):
    return exp


Example(keyword arguments):

def power(num, exp=2):
    return num ** exp


The above program will print the square of 10. The exponent has a default value and the number has a keyword argument.

Arbitrary Argument (*args)

When the developer does not know how many arguments will be passed into the function, we can handle it with an arbitrary function.

We can use an asterisk or symbolical "*" to declare an arbitrary argument. The syntax is:

def my_function(*args):


Keep in mind that Python will take the *args as a tuple of the arguments passed while calling the function.

Example (arbitrary arguments):

def product(*arguments):
    prod = 0
    for i in arguments:
        prod *= i

    return prod

product(1, 2, 3, 4)

The above program will print the product of the numbers 1, 2, 3, and 4.

Arbitrary Keyword Argument (**kwargs):

In the arbitrary functions, only positional arguments were passed. However, for **kwargs or arbitrary keyword argument, we should pass the key and value as the argument.

To be specific, the interpreter takes it as a dictionary, just like args were taken as a list.

Example (keyword arguments):

def my_func(**kwargs):
    for name, age in kwargs.items():
        print(f"{name} is {age} years old")

print(my_func(ankur=17, romisha=18, aakriti=23))

The above program will print the following.

ankur is 17 years old
romisha is 18 years old
aakriti is 23 years old

Recursive Python Functions

Recursion is defining something within itself and hence it runs itself indefinitely unless programmed correctly.

Example (Recursive Functions):

def sum(x):
    if x == 1:
        return 1
        return (x + sum(x-1))

num = 3

The above program prints the sum of digits from 1 to the given number. So, in this program, the sum of digits from 0 to 3 is 6.

Lambda Python Functions

Python has lambda functions that don't need a function name. We can compare it with the anonymous function in JavaScript if you're familiar with the language.

We can directly store the return value of the function in a variable.

Example (lambda functions):

age = int(input("Enter the age: "))
eligibility = lambda age : True if age >= 18 else False  


The above program will input an integer from the user and check if the age is greater or equal to 18 and print True else False.

Type Hints in Python Functions

Type Hints in Python is a way of declaring the data type to accept and return in functions.

We can declare the data type of the parameter in a function. Its syntax is:

def function_name(parameter: data_type):
    return data_type

Here, data_type is pretty explanatory.

Example(type hints):

def square(num: int):
    return num ** 2

The above function will take the "num" integer as an argument. Then, it returns the two-exponent or "square" of the argument "num".

If we pass an argument of a different data type, a TypeError will be raised as usual.

We can also declare the type of data to return when a function is called. The syntax is:

def cube(num: int) -> int:
    return num ** 3

The above function will return an integer which is the cube of an integer "num".

The Official Python Documentation of Function: Click.

You can learn more about other core Python functionalities on our website.