Functions in Python
Functions are one of the fundamental building blocks in Python programming. They are used when you have a block of statements that needs to be executed multiple times within the program. Instead of writing the block of statements repeatedly, you can encapsulate this block within a function, which you can then call from other parts of the program. By defining a function, you write the code once but can execute it any number of times. Functions also help reduce the size of the program by eliminating redundant code. Functions can be categorized into two main types: built-in functions and user-defined functions.
Built-In Functions in Python
Python comes with a comprehensive set of built-in functions that are readily available without the need for any imports. These functions are implemented in C for efficiency and are part of the Python Standard Library. Here's a detailed look at some of the key built-in functions:
-
abs()
- Syntax:
abs(x)
- Description: Returns the absolute value of a number. The absolute value is the distance of a number from zero, without regard to direction. It works with both integers and floating-point numbers.
- Example:
- Syntax:
-
min()
- Syntax:
min(arg_1, arg_2, ..., arg_n)
- Description: Returns the smallest of two or more arguments. It can take multiple arguments or an iterable (like a list) and returns the minimum value among them.
- Example:
- Syntax:
-
max()
- Syntax:
max(arg_1, arg_2, ..., arg_n)
- Description: Returns the largest of two or more arguments. It can take multiple arguments or an iterable (like a list) and returns the maximum value among them.
- Example:
- Syntax:
-
divmod()
- Syntax:
divmod(a, b)
- Description: Takes two numbers and returns a pair of numbers: the quotient and the remainder when dividing
a
byb
. Ifa
andb
are integers, the result is(a // b, a % b)
. If eithera
orb
is a floating-point number, the result is(q, a % b)
, whereq
is the quotient as a whole number. - Example:
- Syntax:
-
pow()
- Syntax:
pow(x, y[, z])
- Description: Returns
x
raised to the power ofy
, which is equivalent tox ** y
. It also supports an optional third argumentz
for modular exponentiation:pow(x, y, z)
returns(x ** y) % z
. - Example:
- Syntax:
-
len()
- Syntax:
len(object)
- Description: Returns the number of items in an object. This can be a string, list, tuple, dictionary, or other iterable types.
- Example:
- Syntax:
-
print()
- Syntax:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
- Description: Outputs data to the standard output (typically the console). It can handle multiple arguments and supports formatting options such as
sep
(separator between items) andend
(string appended after the last value). - Example:
- Syntax:
-
type()
- Syntax:
type(object)
- Description: Returns the type of an object. Useful for debugging and verifying the data type of a variable.
- Example:
- Syntax:
-
int()
,float()
,str()
- Syntax:
int(x)
,float(x)
,str(x)
- Description: Convert values to integer, floating-point, and string types, respectively.
- Examples:
- Syntax:
-
sum()
- Syntax:
sum(iterable, /, start=0)
- Description: Returns the sum of all items in an iterable (e.g., a list of numbers). An optional
start
value can be provided to add to the sum. - Example:
- Syntax:
Commonly Used Modules
Python modules are files containing Python code, which can define functions, classes, and variables. The standard library comes with many modules that provide a wide range of functionalities. Here are some commonly used modules:
-
math
: Provides mathematical functions and constants. Useful for scientific calculations.math.ceil(x)
: Returns the ceiling ofx
, which is the smallest integer greater than or equal tox
.math.sqrt(x)
: Returns the square root ofx
.math.pi
: Represents the mathematical constant π (pi), approximately equal to 3.141592653589793.math.cos(x)
: Returns the cosine ofx
radians.math.factorial(x)
: Returns the factorial ofx
, which is the product of all positive integers less than or equal tox
.math.pow(x, y)
: Returnsx
raised to the power ofy
.
-
datetime
: Handles dates and times. It includes classes likedate
,time
,datetime
, andtimedelta
.Output:
-
random
: Therandom
module generates random numbers and performs random selections. To use its functions, you first need to import the module.Output:
-
os
: Provides a way to interact with the operating system, including file operations and environment variables. -
sys
: Provides access to system-specific parameters and functions, including command-line arguments and Python’s path.
The built-in function dir()
returns a sorted list of strings containing the names of functions, classes, and variables as defined in the module or object. For example, you can find all the functions and attributes supported by the math
module by passing the module name as an argument to the dir()
function.
Example Usage of dir()
:
Output:
This output lists various functions and constants available in the math
module.
Another built-in function you may find useful is help()
, which invokes the built-in help system. The argument to the help()
function can be a module, function, class, method, keyword, or documentation topic, and it prints a related help page to the console.
Example Usage of help()
:
Output:
This output provides information about the gcd()
function in the math
module, explaining that it computes the greatest common divisor of two numbers.
Installing and Using Third-Party Libraries
Third-party modules or libraries can be installed and managed using Python’s package manager pip
.
Installing a Module:
- Explanation: The
pip install arrow
command installs thearrow
library, which provides a user-friendly approach to handling dates and times.
Example Usage of arrow
Library:
Output:
- Explanation:
arrow.utcnow()
returns the current date and time in UTC.- The
now()
method provides the current date and time, including the timezone offset.
Function Definition and Calling Syntax
You can create your own functions and use them wherever needed. User-defined functions are reusable code blocks created by users to perform specific tasks in a program.
Function Definition Syntax
In Python, a function definition consists of the def
keyword followed by:
-
Function Name: The name of the function. The function’s name must follow the same naming rules as variables: it can include letters, numbers, or underscores, but it cannot start with a number. Also, a keyword cannot be used as a function name.
-
Parameters: A list of parameters enclosed in parentheses and separated by commas. Some functions do not have any parameters at all, while others may have one or more parameters.
-
Colon: A colon is required at the end of the function header. The first line of the function definition, which includes the name of the function, is called the function header.
-
Function Body: The block of statements that define the body of the function starts on the next line of the function header and must have the same indentation level.
The def
keyword introduces a function definition. The term "parameter" or "formal parameter" refers to the variables found in the function definition. Defining a function does not execute it; it simply names the function and specifies what to do when it is called. Calling the function actually performs the specified actions with the provided parameters.
Function Call Syntax
The syntax for calling a function is:
Arguments are the actual values passed into the function. There must be a one-to-one correspondence between the formal parameters in the function definition and the actual arguments of the calling function. When a function is called, the formal parameters are temporarily "bound" to the arguments, and their initial values are assigned.
A function should be defined before it is called. The block of statements in the function definition is executed only when the function is called. Normally, statements in a Python program are executed sequentially. Function definitions do not alter the flow of execution of the program. When you call a function, control passes from the calling function to the function definition. After the block of statements in the function definition is executed, control returns to the calling function and proceeds with the next statement. The Python interpreter keeps track of the flow of control between different statements in the program.
Documentation Strings (Docstrings)
The first statement within the function body can optionally be a documentation string, or docstring. Docstrings are used to produce documentation automatically. They are enclosed in triple quotes. For example:
or
When control returns to the calling function from the function definition, the formal parameters and other variables in the function definition no longer contain any values.
Special __name__
Variable
Before executing the code in the source program, the Python interpreter automatically defines a few special variables. If the Python interpreter is running the source program as a stand-alone main program, it sets the special built-in __name__
variable to "__main__"
. After setting up these special variables, the interpreter reads the program to execute the code found at indentation level 0. The block of statements in the function definition is not executed unless the function is called.
The special variable __name__
with the value "__main__"
is the entry point of your program. When the Python interpreter reads the if
statement and sees that __name__
equals "__main__"
, it will execute the block of statements present there.
Example Program
Output:
When coding Python programs, it is best practice to place all relevant calling functions inside the main()
function definition.
Since the above program is a stand-alone main source program, the Python interpreter assigns the string value "__main__"
to the
built-in special variable __name__
, which is checked for equality using the if
condition. If the condition is True
,
the interpreter calls the main()
function. In the main()
function definition, there are two function calls.
You can have any number of function definitions and their calls in your program. When the function at line 8 is called without
any arguments, control flows to the function definition at line 1 and displays the statement at line 2.
After executing the function definition at line 1, control returns to the main()
function and starts executing the next statement. Next, the function call at line 9 with one argument is executed.
Control moves to the function definition at line 4, assigns the string value to the message
parameter,
and displays the passed string value at line 5.
The return
Statement and Void Functions
In Python, functions can return values using the return
statement, or they can be "void" functions that do not return any value. Understanding how to use return
and the concept of void functions is essential for creating effective Python programs.
The return
Statement
The return
statement is used to exit a function and optionally pass an expression back to the caller. When a function is called, it executes the statements in its body until it encounters a return
statement or reaches the end of the function. The value specified by return
is sent back to the caller and can be used in further computations or operations.
Syntax:
- Expression: This is the value or result that you want to return to the caller. It can be any valid Python expression, including variables, literals, or computations.
If no return
statement is present, or if a return
statement does not include an expression, the function returns None
by default.
Example of Function with Return Value:
Output:
In this example, add_numbers
is a function that takes two parameters, computes their sum, and returns the result. In the main
function, this result is captured in the variable sum_value
and then printed.
Void Functions
A "void" function is a function that performs an operation but does not return a value. Such functions are often used for their side effects, like modifying a global variable, printing to the console, or changing the state of an object.
Syntax for Void Functions:
Example of Void Function:
Output:
In this example, print_message
is a void function that prints the provided message to the console. It does not return any value.
Scope of Variables
The scope of a variable refers to the region of the program where the variable is accessible. In Python, the scope of variables is divided into several categories:
-
Local Scope: Variables defined inside a function are local to that function. They are only accessible within that function and are not visible outside of it.
Output:
Note: The second
print
statement will raise aNameError
becauselocal_variable
is not defined in the global scope. -
Global Scope: Variables defined outside of all functions are global variables. They can be accessed from any function within the same module.
Output:
-
Enclosing Scope: When dealing with nested functions, variables in the outer function’s scope are accessible to the inner function. This is also known as the nonlocal scope.
Output:
-
Built-in Scope: This is the scope of built-in names like
print()
,len()
, etc. These are available everywhere in your code.Output:
Variable Lifetime
The lifetime of a variable is the duration for which the variable exists in memory. In Python:
-
Local Variables: Their lifetime begins when the function is called and ends when the function exits. They are created when the function starts and destroyed when the function terminates.
-
Global Variables: Their lifetime lasts as long as the program is running. They are created when the program starts and destroyed when the program terminates.
-
Enclosing Variables: Their lifetime lasts as long as the enclosing function is executing. Once the outer function exits, the enclosing variables are no longer accessible.
Default Parameters
Default parameters allow you to specify default values for function parameters. This means that if a caller does not provide a value for that parameter, the default value will be used.
Syntax:
Example of Default Parameters:
Output:
In this example, greet
has a default parameter name
with the default value "Student"
. If no argument is passed, "Student"
is used; otherwise, the provided argument is used.
Important Points:
-
Order Matters: Parameters with default values must be placed after parameters without default values. This is because Python matches arguments to parameters in order.
-
Mutable Default Values: Be cautious with mutable default values like lists or dictionaries. If you use a mutable default value and modify it, the change will persist across function calls.
To avoid this issue, use
None
as a default value and initialize the mutable object inside the function.
Last updated on -