Home / Blog / Python Functions (with Example)
python function

Python Functions (with Example)

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
Python

Let’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 returns None by default.

Here’s an example of a simple function that adds two numbers:

def add_numbers(a, b):
    return a + b
Python

This 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()
Python
Output
Here is a function

How to Call Python Functions

def my_function():
    print("Here is a function")

my_function() #function call
Python
Output
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")
Python
Output
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")
Python
Output
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")
Python
Output
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:

How to Install Python on Windows | Introduction to Python
A Beginner’s Guide To Python Variables and Types
Python Operators | Types of Operators in Python
Basic Python Syntax | First Program in Python
Python Arrays | List Methods
Dictionaries in Python: A Comprehensive Tutorial
Tuple in Python (with Example)

Post navigation