Which keyword is used for function in python language?

Which keyword is used for function in python language?

In Python programming language, we can create or define a function using the def keyword. In this blog, we will learn how we can use def keyword to play with functions.

Table of contents:

Python def Keyword Explained

The def keyword is used to define a function in a program in Python. A Python function is a set of instructions that are used to perform a certain task. If we are working on a large program, then using functions, we can split large modular codes into functions to improve the readability and reusability of the program.

Defining a Function in Python

While defining a function in Python, we need to follow the below set of rules:

  • The def keyword is used to start the function definition.
  • The def keyword is followed by a function-name which is followed by parentheses containing the arguments passed by the user and a colon at the end.
  • After adding the colon, the body of the function starts with an indented block in a new line.
  • The return statement is used to return something from the function.

Syntax of Python functions using def keyword

def function_name(parameters):
  # function body
  
    return result  

Example

Let’s understand Python functions using an example:

Code:

def add_numbers(num1, num2):
    return num1 + num2

num1= 5
num2= 10
result = add_numbers(num1, num2)
print("The sum = ", result)

Output:

The sum =  15

Explanation: Here we have declared a function that takes two numbers as arguments, and returns the sum of the numbers.

Practical examples of Python def Keyword

Let’s learn some practical examples of Python def Keyword:

1. Find out the maximum of two numbers in Python:

Code:

def find_max(a, b):
    if a > b:
        return a
    else:
        return b

max_num = find_max(10, 20)
print("The maximum number is", max_num)

Output:

The maximum number is 20

Explanation: We have declared a function that takes two arguments and returns the maximum of them.

2. Find the Factorial of a number in Python

Code:

def factorial(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial(n - 1)

result = factorial(4)
print("The factorial =", result)

Output:

The factorial = 24

Explanation: Here we have defined a function factorial, which takes an integer as an argument, and returns the factorial of the number.

If you want to learn more about Python functions, please refer:

Video Thumbnail

Conclusion

So far in this blog, we have learned how to define a function using def keyword, its syntax, and practical examples as well. If you want to learn more about Python, you may refer to our Python Course, which will help you excel in your career.

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner