In this tutorial we want to learn How to Define Functions in Python, 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 talk about the syntax and features of Python function definition.
How to Define Functions in Python?
First of all let’s talk about basic syntax for defining a function, in Python you can define a function like below, 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 "This is codeloop.org" |
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 5 |
def hello_world(): return "This is codeloop.org" print(hello_world()) |
Run the code and you will see this result
Function Parameters in Python
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. the values that are passed are called 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(9,4)) |
Run the code and this will be the result
Default Parameters in Python Functions
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. you can do that 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("codeloop.org")) |
In this example greeting parameter has default value of Hello. if we call greet(“codeloop.org”), function will return Hello, codeloop.org.
This will be the result
Return Value in Python Functions
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 in Python
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__) |
Subscribe and Get Free Video Courses & Articles in your Email