GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • *args
  • Introduction1 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Python Functions - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Python Functions

Last Updated : 19 Sep, 2026

Python functions are reusable blocks of code used to perform a specific task. They help organize programs into smaller sections and execute the same logic whenever needed by calling the function.

why_use_function
Features of a function

Defining a Function

A function can be defined using def keyword. Below is the syntax to define a function:

z_z
Syntax of Python Function Declaration

Here, we define a function using def that prints a welcome message when called.

Python
def fun():
    print("Welcome to GFG")

Calling a Function

After creating a function, call it by using the name of the functions followed by parenthesis containing parameters of that particular function.

Python
def fun():
    print("Welcome to GFG")
    
fun() 

Output
Welcome to GFG

We can also assign a Function to a variable, and call that function using that variable.

Function Arguments

Arguments are values passed to a function when it is called. They allow functions to receive input data and perform operations using those values.

Syntax

def function_name(arguments):
# function body
return value

  • def function_name(arguments): defines a function with optional arguments.
  • # function body contains the statements to be executed.
  • return value returns a result from the function. If no return statement is used, it returns None by default.

Example: In this example, function checks whether the number passed as an argument is even or odd.

Python
def evenOdd(x):
    if (x % 2 == 0):
        return "Even"
    else:
        return "Odd"

print(evenOdd(16))
print(evenOdd(7))

Output
Even
Odd

Types of Function Arguments

Python supports different types of arguments that can be passed during a function call.

1. Default argument: Default argument use a predefined value when no value is passed during the function call.

Python
def myFun(x, y=50):
    print("x: ", x)
    print("y: ", y)

myFun(10)

Output
x:  10
y:  50

Explanation:

  • y=50 sets a default value for parameter y and myFun(10) passes only one argument.
  • Since y is not provided, it uses the default value 50.

2. Keyword Arguments: pass values using parameter names, so argument order does not matter.

Python
def student(fname, lname):
    print(fname, lname)

student(fname='Geeks', lname='Practice')
student(lname='Practice', fname='Geeks')

Output
Geeks Practice
Geeks Practice

Explanation: fname and lname are passed using parameter names and arguments can be provided in any order.

3. Positional Arguments: values are assigned to parameters based on their order in the function call.

Python
def nameAge(name, age):
    print("Hi, I am", name)
    print("My age is ", age)

print("Case-1:")
nameAge("Olivia", 27)

print("Case-2:")
nameAge(27, "Olivia")

Output
Case-1:
Hi, I am Olivia
My age is  27
Case-2:
Hi, I am 27
My age is  Olivia

Explanation:

  • In Case-1, values match the correct parameters.
  • In Case-2, values are swapped because the order changed.

4. Arbitrary Arguments: allow functions to accept multiple values. This is done using two special symbols:

  • *args collects extra positional arguments as a tuple.
  • **kwargs collects extra keyword arguments as a dictionary.
Python
def myFun(*args, **kwargs):
    print("Non-Keyword Arguments (*args):")
    for arg in args:
        print(arg)

    print("Keyword Arguments (**kwargs):")
    for key, value in kwargs.items():
        print(f"{key} == {value}")

myFun('Hey', 'Welcome', first='Geeks', mid='for', last='Geeks')

Output
Non-Keyword Arguments (*args):
Hey
Welcome
Keyword Arguments (**kwargs):
first == Geeks
mid == for
last == Geeks

Explanation:

  • *args stores extra positional arguments.
  • **kwargs stores extra keyword arguments.
  • loop prints all positional and keyword values separately.

Function within Functions

A function defined inside another function is called an inner function (or nested function). It is used to organize related logic and access variables from the outer function.

Python
def f1():
    s = 'I love GeeksforGeeks'
    def f2():
        print(s)
        
    f2()
f1()

Output
I love GeeksforGeeks

Return Statement

Return is used to end a function and send a value back to the caller. It can return any data type, multiple values (packed into a tuple), or None if no value is given.

Syntax

return [expression]

Parameters: expression is the value returned by the function. If no value is returned, it returns None by default.

Python
def sq_value(num):
    return num**2

print(sq_value(2))
print(sq_value(-4))

Output
4
16

Explanation:

  • sq_value(num) accepts a number as input.
  • num**2 calculates the square of the number.
  • return sends the result back to the caller.

Pass by Reference and Pass by Value

Variables refer to objects. Function behavior depends on whether the object is mutable or immutable.

  • Mutable objects like lists can be modified inside functions.
  • Immutable objects like integers and strings remain unchanged.
Python
def myFun(x):
    x[0] = 20

b = [10, 11, 12, 13]
myFun(b)
print(b)

def myFun2(x):
    x = 20

a = 10
myFun2(a)
print(a) 

Output
[20, 11, 12, 13]
10

Explanation:

  • myFun(x) modifies the first element of the list and lists are mutable, so the original list changes.
  • myFun2(x) assigns a new value to x. Integers are immutable, so the original value of a remains unchanged.

Note: Python uses pass-by-object-reference, where functions receive references to objects instead of actual copies.

Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.