Mastering Python
Functions
by Cahya Alkahfi
Mastering Python Function by Cahya Alkahfi
Table of contents
What is a function
Creating a function
Calling a function
Return value
Parameter and argument
*args
**kwargs
Variabel scope
Function as an argument
Function as the return value
Lambda function
Docstring
Mastering Python Function by Cahya Alkahfi
What is a function
A function is a separate code block that performs specific tasks. It allows
us to group related code logically, making development, maintenance, and
code understanding easier.
A function can take input arguments, perform specific operations, and
return values. It is reusable, meaning you can call a function multiple
times from different parts of a program without duplicating code.
Benefits of using functions:
Break complex tasks into smaller, manageable parts
Avoid redundant code by consolidating similar processes into
functions
Improve code readability by organizing it into functional blocks.
Enhance code maintenance by dividing tasks into separate functions
There are many built-in functions in Python. Here are some examples:
Mastering Python Function by Cahya Alkahfi
Creating a function
A function is created using the 'def' keyword followed by the
function name and a pair of parentheses. If the function takes
parameters, they are placed within the parentheses. A function can
also have a return value, marked by the 'return' keyword followed
by the value to be returned
Mastering Python Function by Cahya Alkahfi
Calling a function
A function can be called by using the function name followed by a
pair of parentheses. If the function has parameters, the function's
arguments are placed within the parentheses. If the function has a
return value, the function call can be assigned to a variable to
store its return value.
Mastering Python Function by Cahya Alkahfi
Return Value
A function can return values of any type, ranging from simple types
like int and string to more complex ones like lists, dictionaries,
classes, and even other functions.
Mastering Python Function by Cahya Alkahfi
Parameter and Argument (1/3)
Parameter: A variable declared in the definition of a function.
Parameters are used to receive values that will be used within the
function. Parameters are placed within the parentheses when we
define the function.
Argument: a concrete value provided to a function's parameter
when calling the function. Arguments are placed inside the
parentheses after the function name and are separated by
commas if there is more than one.
more about parameter and argument >>>
Mastering Python Function by Cahya Alkahfi
Parameter and Argument (2/3)
Positional argument: We can omit parameter names as long as the
order of the arguments matches the order of the parameters when
the function is defined.
On the other hand, we can provide arguments in any order as long
as the parameter names are specified
both will produce the same result as before:
more about parameter and argument >>>
Mastering Python Function by Cahya Alkahfi
Parameter and Argument (3/3)
Default Parameter: we can provide default values to function
parameters. When no argument is provided for that parameter, the
default value will be used
Mastering Python Function by Cahya Alkahfi
*args
A special parameter used in a function definition to handle an
unspecified number of arguments. This parameter allows us to
pass in any number of arguments without specifying them
individually in the function definition. The asterisk (*) preceding
args is what makes it special, and we can use any different name
but the asterisk must be present.
Arguments are Collected into a Tuple. We can access these
arguments within the function using the tuple variable named args
or any other variable we assign as the parameter for *args.
Mastering Python Function by Cahya Alkahfi
**kwargs
a special parameter used in a function definition to handle an
unspecified number of keyword arguments. This parameter allows
us to pass a variable number of keyword arguments to a function.
The double asterisks (**) preceding "kwargs" is what makes it
special, and like with *args, we can use a different name than
"kwargs" but the double asterisks must be present. In this case,
arguments are collected into a dictionary.
Mastering Python Function by Cahya Alkahfi
Combining any types of parameters
We can combine various types of parameters in a function. The
rule is simple, all named parameters must come after all positional
parameters
Mastering Python Function by Cahya Alkahfi
Variable Scope (1/2)
variable scope refers to the context in which a variable is defined
and can be accessed. Python defines two main types of variable
scope: global scope and local scope.
local scope variables can only be accessed from the function that
declared it.
Mastering Python Function by Cahya Alkahfi
Variable Scope (2/2)
Enclosing (Non-local) Scope: In some cases, we may have a variable
defined in an enclosing function, and it is accessible to nested
functions.
Global Keyword: If we need to modify a global variable from within
a function, we can use the global keyword to indicate that the
variable being modified is the one in the global scope.
Mastering Python Function by Cahya Alkahfi
Function as an argument (1/2)
In Python, functions are not just blocks of code that you call and
execute; they can also be used as arguments to other functions.
Functions that take other functions as arguments are commonly
referred to as higher-order functions.
Many built-in function in python takes other function as an
argument such as filter and map and many more.
Mastering Python Function by Cahya Alkahfi
Function as an argument (2/2)
Another example:
Mastering Python Function by Cahya Alkahfi
Function as the return value
In Python, functions can also set as the return value for other
function.
Mastering Python Function by Cahya Alkahfi
Lambda function (1/2)
Lambda function, also known as anonymous function, is a concise
way to define small, one-line functions in Python. It is often used
for short, simple operations and is particularly handy in situations
where we need a quick, throwaway function.
lambda: the keyword to define a lambda function,
arguments: input parameters (if any) that the function will accept,
expression: the expression that the function will perform. The result
of this expression is implicitly returned.
Mastering Python Function by Cahya Alkahfi
Lambda function (2/2)
Let‘s redifine our function in the “Function as an argument
section“. Instead of define the ` is_even` and `cubic` functions, we
can directly pass lambda functions to `filter` and `map` functions.
Mastering Python Function by Cahya Alkahfi
Docstring (1/2)
A docstring is a string literal that occurs as the first statement in a
module, function, class, or method definition. It is used to provide
documentation and information about the purpose and usage of
the code. Docstrings are a valuable aspect of Python's self-
documenting nature, aiding developers in understanding and using
code effectively, especially when working with a large project.
Key Elements of a Docstring:
Description: a docstring typically begins with a brief
description of what the code does. This provides a high-level
overview of the code's purpose.
Parameters: If the code defines functions or methods that
accept arguments, the docstring should detail these
arguments, their types, and their purposes.
Return Value: If a function or method returns a value, the
docstring should specify the return type and describe the
meaning of the returned value.
Raises: If the code can raise exceptions, the docstring should
mention the exceptions that may be raised and under what
conditions.
Examples: Including usage examples within the docstring helps
users understand how to use the code effectively.
Mastering Python Function by Cahya Alkahfi
Docstring (2/2)
Example: factorial function with docstring
THANK YOU