In this Python article we want to learn about Python function definition, so if you are a programmer and you do programming in different programming languages, you will know that functions are one of the fundamental building blocks of programming, we can say that function is a block of reusable code that performs specific task, in Python functions are defined using def keyword. in this article we want to explore the syntax and features of Python function definition.
Python function definition
First of all let’s talk about basic syntax for defining a function, in Python you can define a function like this, now let’s me describe this code, def keyword is followed by the name of the function and pair of parentheses. any parameters that the function takes are listed inside parentheses. body of the function is indented and contains statements that the function executes.
1 2 3 |
def function_name(parameters): """Docstring""" statements |
This is an example of a simple function that takes no parameters and returns a string like this.
1 2 |
def hello_world(): return "Welcome to geekscoders.com" |
So right now we have our function, but we can not see the result, because when you define a function, you need to call that function in your code like this.
1 2 3 4 |
def hello_world(): return "Welcome to geekscoders.com" print(hello_world()) |
This will be the result

Now let’s talk about function parameters, functions in Python can take one or more parameters. Parameters are listed inside parentheses when the function is defined. when function is called, values can be passed in for parameters. values passed in are referred to as arguments.
This is the example of two parameters that returns the sum of x and y.
1 2 3 4 5 |
def add_numbers(x, y): return x + y print(add_numbers(5,8)) |
Run the code and this will be the result

There is another thing that you can use in Python Function and that is called default parameter, sometimes we want a parameter to have a default value if no argument is passed in. this can be accomplished by specifying a default value for the parameter in the function definition.
This is an example of a function that takes one required argument and one optional argument:
1 2 3 4 |
def greet(name, greeting="Hello"): return f"{greeting}, {name}" print(greet("geekscoders.com")) |
In this example greeting parameter has default value of Hello. if we call greet(“geekscoders.com”), function will return Hello, geekscoders.com.
This will be the result

if we call greet(“geekscoders”, “Good morning”), function will return Good morning geekscoders like this.

Functions in Python can return a value using return keyword. when return keyword is encountered, function stops executing and returns the value specified.
1 2 3 4 |
import math def circle_area(radius): return math.pi * radius ** 2 |
Docstrings are used to provide documentation for functions. they are enclosed in triple quotes and placed immediately after the function header. Docstrings should describe what function does, what parameters it takes, and what it returns.
1 2 3 |
def add_numbers(x, y): """Return the sum of two numbers.""" return x + y |
For accessing the docstring of a function, we can use __doc__ attribute like this:
1 |
print(add_numbers.__doc__) |