Python functions
Functions in Python
A function in Python is a reusable block of code that performs a specific task. Functions help organize code, promote code reusability, and make it easier to manage and understand complex programs. You can define your own functions or use built-in functions provided by Python.
Defining a Function
You can define a function using the def
keyword, followed by the function name and parentheses. Any parameters the function takes are specified within the parentheses. The function body is indented below the definition.
Syntax
function_name
: The name you choose for the function.parameters
: Input values (arguments) the function can accept; these are optional.return
: A statement used to return a value from the function (optional).
Example 1: A Simple Function
Output:
In this example, the greet
function prints "Hello, world!" when called.
Example 2: Function with Parameters
You can define a function that accepts parameters to customize its behavior.
Output:
Here, the greet
function takes one parameter (name
) and uses it to print a personalized greeting.
Example 3: Function with Return Value
A function can return a value using the return
statement.
Output:
In this example, the add
function takes two parameters and returns their sum.
Example 4: Function with Default Parameter Values
You can define default values for parameters in a function. If the caller does not provide a value, the default is used.
Output:
Here, if no name is provided, the function defaults to "Guest".
Example 5: Variable-Length Arguments
You can define functions that accept a variable number of arguments using *args
for non-keyword arguments and **kwargs
for keyword arguments.
Using *args
Output:
In this example, the sum_all
function can take any number of positional arguments.
Using **kwargs
Output:
Here, print_info
can take any number of keyword arguments and prints them.
Example 6: Lambda Functions
Python also supports anonymous functions (lambda functions), which are small, one-line functions defined using the lambda
keyword.
Syntax
Example of a Lambda Function
Output:
In this example, add
is a lambda function that takes two arguments and returns their sum.
Scope of Variables in Functions
- Local Variables: Variables defined inside a function are local to that function and cannot be accessed outside.
- Global Variables: Variables defined outside of functions can be accessed inside functions, but you must declare them as
global
if you want to modify them.
Example of Local and Global Variables
Output:
In this example, the global variable x
is modified inside the modify
function.
Summary
- Functions are reusable blocks of code defined using the
def
keyword. - They can accept parameters, return values, and have default parameter values.
- You can define functions with variable-length arguments using
*args
and**kwargs
. - Lambda functions provide a way to create small anonymous functions.
- Understanding the scope of variables is important when working with functions.
Functions are essential for organizing and structuring code in Python, making it modular and easier to read and maintain.