My Blog

PYTHON FUNCTIONS TUTORIAL

What is a function in Python?

In Python, a function is a set of related statements that perform a specific task. Functions help break down our program into smaller and modular parts. As our program grows, functions make it more organized and manageable.

Above code explanation:

Keyword def that marks the start of the function header.

A colon (:) to mark the end of the function header.

Optional documentation string (docstring) to describe what the function does.

How to call a function in python?

Once a function is created in Python, we can call it by writing functionName() itself or another function/nested function. Following is the syntax for calling a function.

The return statement

The Python return statement is a special statement that you can use inside a function or method to send the function’s result back to the caller. A return statement consists of the return keyword followed by an optional return value.

ARGUMENTS IN PYTHON

Functions accept arguments that can contain data. The function name is followed by parenthesis that list the arguments. Simply separate each argument with a comma to add as many as you like.

Example 1: Python Function Arguments:

In the above example, the function add_numbers() takes two parameters: a and b.

Function Argument with Default Values:

In Python, we can provide default values to function arguments. We use the = operator to provide default values

In the above example, the function definition Here, we have provided default values 7 and 8 for parameters a and b respectively.

Python Keyword Argument

A function must always be called with the appropriate number of parameters by default. In other words, if your function requires 2 arguments, you must call it with 2 arguments, not more or fewer.

First name from the function declaration is paired with first name from the function call. Similar to how last name in the function specification corresponds to last name in the function call.

Recursive Function

What is recursion?

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.

Python Recursive Function

In Python, we know that a function can call other functions. It is even possible for the function to call itself. These types of construct are termed as recursive functions

Factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 3 (denoted as 3!) is 3*2*1. Quesition is that how to implement this function in python?

In the above example, factorial() is a recursive function as it calls itself. When we call this function with a positive integer, it will recursively call itself by decreasing the number. Each function multiplies the number with the factorial of the number below it until it is equal to one.

Our recursion ends when the number reduces to 1. This is called the base condition. Every recursive function must have a base condition that stops the recursion or else the function calls itself infinitely.