A function is a block of code that performs a specific task whenever it is called. In bigger programs, where we have large amounts of code, it is advisable to create or use existing functions that make the program flow organized and neat.
These functions are defined and pre-coded in python. Some examples of built-in functions are as follows:
min(), max(), len(), sum(), type(), range(), dict(), list(), tuple(), set(), print(), etc.
We can create functions to perform specific tasks as per our needs. Such functions are called user-defined functions.
def function_name(parameters):
pass
# Code and Statements
We call a function by giving the function name, followed by parameters (if any) in the parenthesis.
Example:
def name(fname, lname):
print("Hello,", fname, lname)
name("Sam", "Wilson")
Output:
Hello, Sam Wilson