Python Function with Parameters


Function with Parameters in Python

A function with parameters is a function that can take inputs (arguments) when it is called. This allows you to pass data into the function, making it more flexible and reusable. Parameters act as placeholders for the values you provide when calling the function.

Defining Parameters

You can define a function with one or more parameters by including them in the parentheses after the function name. The parameters are specified in the function definition and can be used within the function body.

Syntax

def function_name(parameter1, parameter2, ...): # Code block (function body) return value # Optional

Example of a Function with Parameters

Here’s a simple example of a function that takes two parameters:

def greet(name, age): """This function greets a person with their name and age.""" print(f"Hello, {name}! You are {age} years old.") # Calling the function with arguments greet("Alice", 30)

Output:

Hello, Alice! You are 30 years old.

Breakdown of the Example

  1. Function Definition:

    • The function greet is defined with two parameters: name and age.
    • The parameters are used within the function to create a greeting message.
  2. Function Call:

    • The function is called with the arguments "Alice" and 30, which are passed to the name and age parameters, respectively.
    • Inside the function, these parameters are used to generate the greeting message.

Default Parameter Values

You can also define default values for parameters, which allows you to call the function without providing all the arguments. If an argument is not provided, the default value will be used.

def greet(name="Guest", age=18): """This function greets a person with their name and age (with default values).""" print(f"Hello, {name}! You are {age} years old.") # Calling the function with different combinations of arguments greet() # Uses default values greet("Bob") # Uses default age greet("Charlie", 25) # Uses provided values

Output:

Hello, Guest! You are 18 years old. Hello, Bob! You are 18 years old. Hello, Charlie! You are 25 years old.

Keyword Arguments

You can also call functions using keyword arguments, which allow you to specify arguments by name, regardless of their order. This can make your code clearer.

greet(age=25, name="David")

Output:

Hello, David! You are 25 years old.

Variable-Length Arguments

Sometimes you may not know how many arguments you want to pass to a function. In such cases, you can use variable-length arguments:

Using *args

This allows you to pass a variable number of non-keyword arguments to a function:

def sum_all(*args): """This function returns the sum of all provided arguments.""" return sum(args) # Calling the function with different numbers of arguments print(sum_all(1, 2, 3)) # Output: 6 print(sum_all(1, 2, 3, 4, 5)) # Output: 15

Using **kwargs

You can also use **kwargs to accept a variable number of keyword arguments:

def print_info(**kwargs): """This function prints all provided keyword arguments.""" for key, value in kwargs.items(): print(f"{key}: {value}") # Calling the function with keyword arguments print_info(name="Eve", age=22, city="Paris")

Output:

name: Eve age: 22 city: Paris

Summary

  • Parameters allow functions to accept input values, making them reusable and flexible.
  • You can define functions with one or more parameters, and you can also provide default values for parameters.
  • Keyword arguments allow you to specify arguments by name, improving code readability.
  • Use *args for variable-length non-keyword arguments and **kwargs for variable-length keyword arguments.

Functions with parameters are essential for writing modular and maintainable code in Python, enabling you to pass different values and customize behavior without rewriting code.