Functions

A function is a block of statements which is organised, reusable and run only when it’s called. Python includes a large number of built-in functions, such as print(), input(), and others, but you can also make your own functions. These functions are called user-defined functions.

Syntax of Python Function

def func_name( parameters ):
   statement(s)
   return [expression]

func_name();

Parameters and [expression] are options in creating functions but highly used in production programming.

Let’s take a look at the simplest function you can write in python.

Example#1 (Simplest form of function)

def function ():
    print("its a function")

function()

Output:
its a function

The above function is not using any parameter or return statement, which are optional.

Arguments of a function

Information that is passed into a function can be referred to as both a parameter and an argument. From the viewpoint of a function: The variable in the function definition that is listed between parentheses is referred to as a parameter. The value passed to a function when it is called is known as an argument.

Example#2 (Function to add two numbers)

def add_num (first_num, second_num):
   sum = first_num+second_num
   print(sum)

add_num(4,5)
add_num(100, 400)


Output:

9
500

The above program is taking two parameters (first_num and second_num), while calling this function you have to pass the values in parentheses.

Now let’s make this program a little shorter.

def add_num (first_num, second_num):
   print(first_num + second_num)

add_num(3,6)

Example#3 (Function to take Cubic of integer)

def cubic (num):
   c= num*num*num
   print('cubic of', num, 'is',c)

cubic(3)
cubic(7)

Output:

cubic of 3 is 27
cubic of 7 is 343

In the above program, variable num is an argument/parameter in cubic function.

Return Statement in Python

The function call is terminated with a return statement, which also “returns” the outcome (the value of the expression that follows the return keyword) to the caller. After the return statements, the following statements are not carried out. The special value None is returned if the return statement contains no expression. Invoking a function generally involves using a return statement so that the passed statements can be carried out.

Note: You cannot use the return statement outside of the function.

def cubic(x):
    b = x*x*x
    return b

print(cubic(5))

a = cubic(5) + 1
print(a)
Output:

125
126

Recursive Function

Additionally, a function in Python has the ability to call itself! Recursion is the process of using a recursive function. A recursive function is one that calls itself.

Here is the simplest form of recursive function:

def fun():
    print("recurse")
    fun()

fun()
Output:
recurse
recurse
recurse
.
.
.
RecursionError: maximum recursion depth exceeded while calling a Python object

The above function doesn’t work long and you will get a recursion error, as when a function calls itself in the above program it goes forever.

Example#4 (Recursion Function)

def cubic_down(x):
    if x>0:
        y = x*x*x
        print(y)
        x=x-1
        cubic_down(x)
    else:
        return

cubic_down(5)

Output:

125
64
27
8
1

The above recursive program calls itself until count reaches to zero, however you must have to once call function outside its scope.

The Benefits of Recursion

1. Code that uses recursive functions appears organized and beautiful.

2. Recursion allows for the breakdown of a challenging assignment into smaller, more manageable issues.

3. Sequence generation is easier with recursion than with nested iteration.

Negative Effects of Recursion

1. It can be challenging to comprehend recursion’s logic at times.

2. Recursive calls are expensive because they consume a lot of time and memory.

3. Debugging recursive functions is challenging.

Anonymous Functions

An anonymous function in Python is one that has no name when it is defined. Instead of using the def keyword, which is used to define conventional functions, Python uses the lambda keyword to define anonymous functions. Lambda functions are therefore another term for anonymous functions.

Syntax of Anonymous Function

Generally, the syntax of lambda function is following:

lambda arguments: expression

But you can rely on this syntax because you need a variable to call a recursive function.

var = lambda arguments : expression

To call a recursive function you have to call its variable.

var(arguments)

Example (Anonymous Function)

cubic = lambda num: num * num * num

print(cubic(5))

Output:

125

You can see that making a cubic function using lambda is simpler than user-defined functions.

Categorized in: