Python functions are block of organized, reusable code that perform a specific task. They help in modularizing code, making it easier to read, understand, and maintain. Functions in Python are defined using the def
keyword followed by the function name and parentheses ()
. If the function takes any parameters, they are placed within the parentheses.
Here’s a basic syntax for defining a function:
def function_name(parameter1, parameter2, ...):
# Function body - code to perform a task
# It may include variable declarations, conditional statements, loops, etc.
# Optionally, the function may return a value using the return statement
return something
PythonLet’s break down the components of a function:
def
: It’s a keyword used to define Python functions.function_name
: This is the identifier for the function. You can name your function anything as long as it follows Python’s identifier naming rules.parameter1, parameter2, ...
: These are optional parameters that the function accepts. They act as placeholders for values that will be passed into the function when it is called.return something
: This is an optional statement that specifies the value that the function should return after it’s executed. If omitted, the function returnsNone
by default.
Here’s an example of a simple function that adds two numbers:
def add_numbers(a, b):
return a + b
PythonThis function can be called by using their name followed by parentheses and passing required arguments (if any).
Example of Python Function
def my_function():
print("Here is a function")
my_function()
PythonOutput Here is a function |
How to Call Python Functions
def my_function():
print("Here is a function")
my_function() #function call
PythonOutput Here is a function |
Function Arguments
We can pass information as arguments into a function:
Example
def my_function(num):
print(str(num) + "key")
my_function("One")
my_function("Two")
my_function("Three")
PythonOutput Onekey Twokey Threekey |
Arbitrary Arguments, *args
If you have no idea about how many arguments will be passed into your function we can use * before the name of the argument.
Example
def my_function(*num):
print("We will print number " + num[2])
my_function("One", "Two", "Three")
PythonOutput We will print number Three |
This code defines a function my_function
that takes a variable number of arguments (*num
). Inside the function, it prints the third argument passed to it. When the function is called with "One", "Two", "Three"
, it prints "Three"
because it’s the third argument in the list.
Keyword Arguments
We can use keyword arguments and assign values to these keywords individually.
def my_function(num1, num2, num3):
print("We will print number " + num3)
my_function(num1="One", num2="Two", num3="Three")
PythonOutput We will print number Three |
This code defines a function my_function
that takes three arguments (num1
, num2
, num3
). Inside the function, it prints the value of num3
. When the function is called with "One"
, "Two"
, and "Three"
as arguments, it prints "Three"
.
Author: TCF Editorial
Copyright The Cloudflare.
Related topics: