In Python, a function is a named block of code that performs a specific task. Functions can take input arguments and return output values. They are a fundamental concept in programming that allow you to break down complex tasks into smaller, more manageable pieces.
Defining a Function
To define a function in Python, you use the def
keyword followed by the function name and any input arguments in parentheses. The function body is indented below the function definition. For example:
def greet(name): print("Hello, " + name + "!")
This function takes one input argument, name
, and prints a greeting message using that name.
Calling a Function
To call a function in Python, you simply use the function name followed by any input arguments in parentheses. For example:
greet("Alice")
This code will call the greet
function with the input argument "Alice"
.
Output
“Hello, Alice!”
Returning a Value
Functions can also return output values using the return
keyword. For example, the following function takes two input arguments and returns their sum:
def add(x, y): return x + y
To use the output value of a function, you can assign it to a variable or use it in an expression. For example:
result = add(3, 4) print(result)
Output
7
Default Arguments
You can specify default values for function arguments by assigning a value to the argument in the function definition. If a value is not provided for the argument when the function is called, it will use the default value instead. For example:
def greet(name="World"): print("Hello, " + name + "!")
This function has a default value of "World"
for the name
argument. If the function is called without an argument, it will use this default value:
greet()
Output
“Hello, World!”
Variable-Length Arguments
Functions can take a variable number of arguments by using the *args
syntax. This allows you to pass any number of positional arguments to the function, which are then collected into a tuple. For example:
def multiply(*args): result = 1 for arg in args: result *= arg return result
This function takes any number of input arguments and multiplies them together. For example:
result = multiply(2, 3, 4) print(result)
Outputs
24
Keyword Arguments
Functions can also take keyword arguments, which are specified as key=value
pairs in the function call. These arguments are collected into a dictionary. For example:
def greet(name, age): print("Hello, " + name + "! You are " + str(age) + " years old.") greet(name="Alice", age=30)
This code calls the greet
function with two keyword arguments, "name"
and "age"
.
Output
“Hello, Alice! You are 30 years old.”
Lambda Functions
In Python, you can also define functions using the lambda
keyword, which creates an anonymous function. Lambda functions are useful for short, one-off functions that don’t need a name. For example:
multiply = lambda x, y: x * y result = multiply(3, 4) print(result)
Output
12
This code defines a lambda function that takes two input arguments, x
and y
, and returns their product. The lambda function