Functions and Exception Handling
Functions and Exception Handling
Definition
A function is a block of code which only runs when it
is called.
You can pass data, known as parameters, into a
function.
A function can return data as a result.
Types of Functions
Basically, we can divide functions into the following two types:
Built-in functions -
Functions that are built into Python.
User-defined functions -
Functions defined by the users themselves.
Creating a Function
using the def keyword:
Example
def my_function():
print("Hello from a function")
Calling a Function
my_function()
Working with Parameters
A parameter is a named entity in a function definition, specifying an
argument that the function can accept.
Let’s create a small program that takes in parameters x, y, and z.
This function can have any number of arguments but only one expression,
which is evaluated and returned.
One is free to use lambda functions wherever function objects are required.
You need to keep in your knowledge that lambda functions are syntactically
restricted to a single expression.
It has various uses in particular fields of programming besides other types of
expressions in functions.
Example:
Program to show the use of lambda functions
double = lambda x: x * 2
# Output: 10
print(double(5))