DEV Community

Cover image for Exploring Python Functions: A Comprehensive Guide
Karthick (k)
Karthick (k)

Posted on

Exploring Python Functions: A Comprehensive Guide

A function in Python is a block of code that enables a specific task to be performed multiple times within a program without needing to rewrite the code. This approach helps reduce code duplication, enhances readability and organisation, breaks a program into smaller, manageable sections, and simplifies debugging and testing.

Defining a Function

Python functions are defined using the def keyword, followed by the function name and parentheses.

# Define a function
def hello():
  print("Hello, world!")

# Call the function
hello()

Enter fullscreen mode Exit fullscreen mode

In this example, the hello() function prints the given message to the console.

Here is the output:

Hello, world!

Defining a Function with Parameters

A Python function can be defined with parameters, which help in passing data to the function.

def hello(name):
    print(f'hello, {name}')


hello("Karthick")
Enter fullscreen mode Exit fullscreen mode

Here is the output:

hello, Karthick

Note: Function names in Python are written in snake_case.

Returning Values from a Function

In Python, a value can be returned from a function in two ways.

  1. Using the return keyword
  2. Using the yield keyword

Returning Values Using return

The return keyword returns a value from a Python function. You can then store the returned value in a variable and use it in the program.

def check_leap_year(year):
    if year%4==0:
        return str(year) + " is a leap year!"
    else:
        return str(year) + " is not a leap year!"

year_to_check=2020;

return_values=check_leap_year(year_to_check);

print(return_values)

Enter fullscreen mode Exit fullscreen mode

Here is the output:

2020 is a leap year!

Returning Values Using yield

A function can also return values with the yield keyword, like a ** return ** statement. Unlike return, the yield statement retains the state of the function and will resume where it left off on the next function call (i.e., execution resumes after the last yield statement). This way, the function can produce a number of values over time.

# Function to produce infinite Fibonacci numbers
def fibonacci():
  # Generate first number
  a = 1
  yield a

  # Generate second number
  b = 2
  yield b

  # Infinite loop
  while True:
    # Return sum of a + b
    c = a + b
    yield c

    # Function resumes loop here on next call
    a = b
    b = c

# Iterate through the Fibonacci sequence until a limit is reached
for num in fibonacci():
  if num > 50:
    break

  print(num)
Enter fullscreen mode Exit fullscreen mode

Here is the output:

1
2
3
5
8
13
21
34

Frequently Asked Questions

  1. What’s the difference between a parameter and an argument in a Python function?

  2. What happens if you call a function before defining it?

  3. What is a default parameter in a Python function?

Top comments (0)