Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Creating a Function in Python
3.
Calling a Function in Python
3.1.
Python
4.
Frequently Asked Questions
4.1.
How do I define a function in Python?
4.2.
How do I pass arguments to a function in Python?
4.3.
What is a return statement in Python functions?
5.
Conclusion
Last Updated: Jun 30, 2024
Easy

How to Create a Function in Python

Introduction

Functions are an essential aspect of Python programming. They allow you to bundle related code together and reuse it, which improves the efficiency and readability of your program. Additionally, functions help in structuring and maintaining your code more effectively.

How to Create a Function in Python

In this article, we will explore how to create a function in Python. We will cover the basics of function creation, discuss various types of functions. This guide will provide you with the foundational knowledge needed to define and utilize functions in your Python programs.

Creating a Function in Python

To create a function in Python, you follow these steps:

  1. Use the "def" keyword followed by the function name.
     
  2. Add a set of parentheses after the function name.
     
  3. If the function takes parameters, specify them inside the parentheses.
     
  4. Add a colon after the parentheses to indicate the start of the function block.
     
  5. Indent the function body (the code inside the function) with four spaces or a tab.
     
  6. Optionally, use the "return" statement to specify the value(s) the function should return.
     

Example that shows the process of creating a function:

def add_numbers(a, b):
sum = a + b
return sum


In this example, we created a function named "add_numbers" that takes two parameters, "a" and "b". The function calculates the sum of these two numbers and assigns it to the variable "sum". Finally, the function uses the "return" statement to send the value of "sum" back to the caller.

Let's create another function that greets a person by name:

def greet_person(name):
greeting = "Hello, " + name + "!"
print(greeting)


This function, named "greet_person", takes a single parameter "name". Inside the function, we create a greeting message by concatenating the string "Hello, " with the value of "name" and an exclamation mark. The function then prints the greeting message using the "print()" function.

Calling a Function in Python

Once you have defined a function, you can call it from anywhere in your program. To call a function, you simply use the function name followed by a set of parentheses. If the function requires arguments, you pass them inside the parentheses.

Let's see how to call the functions we created in the previous examples:

  • Calling the add_numbers function
result = add_numbers(5, 3)
print(result)  


Output 

8

 

  • Calling the greet_person function
greet_person("Rahul")  


Output: 

Hello, Rahul!


In the first example, we call the "add_numbers" function and pass two arguments, 5 and 3, inside the parentheses. The function returns the sum of these numbers, which is then assigned to the variable "result". We print the value of "result" to display the sum.

In the second example, we call the "greet_person" function and pass the name "Rahul" as an argument. The function prints the greeting message "Hello, Rahul!" to the console.

You can call a function multiple times with different arguments to perform the same operation on different data. For example:

  • Calling the add_numbers function multiple times
  • Python

Python

result1 = add_numbers(2, 3)

result2 = add_numbers(10, 7)

result3 = add_numbers(4, 9)

print(result1) 

print(result2) 

print(result) 

Output:

5 
17
13

 

In this example, we call the "add_numbers" function three times with different pairs of numbers. Each time, the function returns the sum of the provided numbers, and we assign the results to different variables for later use.

Frequently Asked Questions

How do I define a function in Python?

To define a function in Python, use the def keyword followed by the function name and parentheses. You can optionally include parameters inside the parentheses, and the function body is indented below the definition.

How do I pass arguments to a function in Python?

Arguments are passed to a function by specifying them within the parentheses when defining the function and when calling it.

What is a return statement in Python functions?

 A return statement exits a function and optionally returns a value to the caller. If omitted, the function returns None by default.

Conclusion

In this article, we thoroughly discussed on how to create a function in python. Later in the blog, we also discussed on how you can call a function in Python. 

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass