How to Define and Call a Function in Python Last Updated : 16 Dec, 2024 Comments Improve Suggest changes Like Article Like Report In Python, defining and calling functions is simple and may greatly improve the readability and reusability of our code. In this article, we will explore How we can define and call a function.Example: Python # Defining a function def fun(): print("Welcome to GFG") # calling a function fun() Let's understand defining and calling a function in detail:Defining a FunctionBy using the word def keyword followed by the function's name and parentheses () we can define a function. If the function takes any arguments, they are included inside the parentheses. The code inside a function must be indented after the colon to indicate it belongs to that function.Syntax of defining a function:def function_name(parameters):# Code to be executedreturn valuefunction_name: The name of your function.parameters: Optional. A list of parameters (input values) for the function.return: Optional. The return statement is used to send a result back from the function.Example: Python def fun(): print("Welcome to GFG") # Defining a function with parameters def greet(name, age): print(name, age) Explanation:fun(): A simple function that prints "Welcome to GFG" when called, without any parameters.greet(name, age): A function with two parameters (name and age) that prints the values passed to it when called.Calling a FunctionTo call a function in Python, we definitely type the name of the function observed via parentheses (). If the function takes any arguments, they may be covered within the parentheses . Below is the example for calling def function Python.Syntax of Calling a function:function_name(arguments)Example: Python def fun(): print("Welcome to GFG") #calling a function fun() # Defining a function with parameters def greet(name, age): print(name, age) #calling a function by passing arguments greet("Alice",21) OutputWelcome to GFG Alice 21 Explanation:Calling fun(): The function fun() is called without any arguments, and it prints "Welcome to GFG".Calling greet("Alice", 21): The function greet() is called with the arguments "Alice" and 21, which are printed as "Alice 21". Comment More infoAdvertise with us Next Article How to Define and Call a Function in Python V vishakshx339 Follow Improve Article Tags : Python python Practice Tags : pythonpython Similar Reads How to Call a C function in Python Have you ever came across the situation where you have to call C function using python? This article is going to help you on a very basic level and if you have not come across any situation like this, you enjoy knowing how it is possible.First, let's write one simple function using C and generate a 2 min read How to call a function in Python Python is an object-oriented language and it uses functions to reduce the repetition of the code. In this article, we will get to know what are parts, How to Create processes, and how to call them.In Python, there is a reserved keyword "def" which we use to define a function in Python, and after "de 5 min read How to Add Function in Python Dictionary Dictionaries in Python are strong, adaptable data structures that support key-value pair storage. Because of this property, dictionaries are a necessary tool for many kinds of programming jobs. Adding functions as values to dictionaries is an intriguing and sophisticated use case. This article looks 4 min read How to pass an array to a function in Python In this article, we will discuss how an array or list can be passed to a function as a parameter in Python. Pass an array to a function in Python So for instance, if we have thousands of values stored in an array and we want to perform the manipulation of those values in a specific function, that is 4 min read How to Call Multiple Functions in Python In Python, calling multiple functions is a common practice, especially when building modular, organized and maintainable code. In this article, weâll explore various ways we can call multiple functions in Python.The most straightforward way to call multiple functions is by executing them one after a 3 min read Define and Call Methods in a Python Class In object-oriented programming, a class is a blueprint for creating objects, and methods are functions associated with those objects. Methods in a class allow you to define behavior and functionality for the objects created from that class. Python, being an object-oriented programming language, prov 3 min read How to Recall a Function in Python In Python, functions are reusable blocks of code that we can call multiple times throughout a program. Sometimes, we might need to call a function again either within itself or after it has been previously executed. In this article, we'll explore different scenarios where we can "recall" a function 4 min read How to use Function Decorators in Python ? In Python, a function can be passed as a parameter to another function (a function can also return another function). we can define a function inside another function. In this article, you will learn How to use Function Decorators in Python. Passing Function as ParametersIn Python, you can pass a fu 3 min read Defining a Python Function at Runtime One amazing feature of Python is that it lets us create functions while our program is running, instead of just defining them beforehand. This makes our code more flexible and easier to manage. Itâs especially useful for things like metaprogramming, event-driven systems and running code dynamically 3 min read Call a function by a String name - Python In this article, we will see how to call a function of a module by using its name (a string) in Python. Basically, we use a function of any module as a string, let's say, we want to use randint() function of a random module, which takes 2 parameters [Start, End] and generates a random value between 3 min read Like