In this file, MarsDevs illustrates functions in Python, the function argument, and the keyword argument. We use various examples with code to understand the concept better.
Click here to know more: https://www.marsdevs.com/blogs/functions-and-arguments-in-python
Unlocking the Potential of the Cloud for IBM Power Systems
Functions and Arguments in Python
1. Functions and Arguments in
Python
Published on: September 20, 2022
In this article, MarsDevs illustrates functions in Python, the function
argument, and the keyword argument. We use various examples with
code to understand the concept better.
What do we mean by a Function?
A function is a block of code that performs certain defined operations
which can be reused by calling that function. So, a function has a high
degree of code reuse capacity and better modularity.
When a function is created, we define the sequence of steps that should
be performed and return its resultant or performed value to the caller. We
call this function by the name of the function.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
2. Python has already built-in functions like print(), sqrt(), etc. We can also
create our own functions, so created functions by users are called user-
defined functions.
User-defined function and built-in function
Defining a Function
We can create a function by defining the required functionality into it. We
need to remember some rules before creating functions using Python.
These rules are as follows
Starting with the keyword def then naming the function with
parentheses ().
Name of the function should begin with a letter or underscore or
lowercase letters.
Name of the function can have digits but the function name can not
start with a digit.
Name of the function can be in any length and can not be the same
as Python keywords.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
3. You can define or place input parameters or arguments inside these
parentheses ().
There is an optional statement - docstring or documentation string of
the function.
Then the code block starts after a: after parentheses ().
Return statement is also optional which returns the control to the
caller function.
Syntax of function
There should be positional behavior to return in the same order as they
defined.
Example
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
4. The function definition consists of name, parameters required and
required step in which manner function should go, then return values, if
any.
def keyword - Tells the compiler that function starts from here.
Function name - Give the function a name to avoid ambiguity and
call using this name.
Parameters - These are the arguments passed to the function and
used in the function processing.
Colon (:) - Marks end of a function declaration
Docstring - This is a comment which tells what this function does.
Docstring makes it easy to understand the code. It’s optional to use.
Statements - These are the steps in the function which dictate what
a function should do.
return - Returns the resultant value or expression to the caller of this
function, if not mentioned it returns None. It’s optional to use.
Calling a function
We call a function by its name with parentheses () and if there are any
parameters then we put those parameters inside these parentheses ().
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
5. Example
Argument Passing Techniques
There are different types of techniques to pass arguments to a function.
We will discuss 2 argument passing techniques here: pass-by-value and
pass-by-reference.
In pass-by-reference, the address of the variables/data structures
are passed by the calling-function, so any changes done by the
called-function in these parameters will be reflected in the original
copy in the calling-function.
In pass-by-value, a copy of only the value of variables/data
structures is passed to the called-function and if there is any change
made by called-function in the copied parameters those will not be
reflected in the original copy in the calling-function.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
6. Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
7. In pass-by-reference argument passing technique, when we change
parameters that are referenced from the caller function within a called-
function, the change also reflects back in the calling / original function.
This is because addresses of those parameters are passed instead of
values to the called-function. Python uses this argument passing
technique.
In the pass-by-value argument passing technique, when we change
parameters referenced from caller function within a called-function, the
change does not reflect back in the calling / original function. This is
because only the copies of those values of that argument are passed
instead of addresses.
Function Arguments
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
8. Now, let’s try to understand arguments that are passed to a function.
There are various types of functions arguments
Required/Positional arguments
Keyword arguments
Default arguments
Variable-length arguments
These are explained as following below in brief
1. Required arguments
These arguments should be passed in the correct positional order, so they
should match a number of arguments in the function call with the function
definition. They should be defined in an ordered manner.
Example
The above mess() function requires one argument (name) to pass in
function, there will be an error raised without it, modified example is given
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
9. as following below
2. Keyword arguments
When you pass these arguments, then the caller identifies the arguments
by parameter name. These are not mandatory to pass the arguments or
place them in order like required/positional arguments. Python
interpreters can identify and match the values with parameters.
Take this example
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
10. If you want to label them in first_name and last_name in the calling-
function, then you can label as below
Here first_name and last_name are keyword arguments
3. Default arguments
When you do not provide any value for an argument then it assumes a
default value (you can set) in the function call for that argument. Take the
following example
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
11. Note that when you do not know how many a number of arguments
should be passed, then we use two asterisks **name, to map arguments
to a single parameter. Consider the following example
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
12. 4. Variable-length arguments
In this, there could be less number of parameters in the function definition
than the number of arguments passed. These parameters are not named
in the function definition and are known as variable-length arguments. An
example is given below
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
13. Recursive function
Recursive function calls, again and again, the same function till the base
condition is satisfied or stack overflows / memory overflows.
Consider the following recursive function to calculate the factorial of
natural number n.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
14. Anonymous function
These functions are defined without a name and use the “lambda”
keyword instead of “def”, so these are also called as a lambda function.
They return only one value but can have 0 or more arguments.
Syntax is given below
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
15. If we were to write above logic using normal function, then it would be
Scope of Variables in Python
There are 2 types of variables, based on their scope - Global variable and
Local variable.
1. Global Variable
These variables are declared outside of the functions and can be
accessed globally by any function defined in the main function or
program. These can be accessed inside or outside of the function, i.e
everywhere.
An example is given as follows below
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
16. 2. Local Variable
These variables are declared inside the function and can be accessed
within that function only. These can not be accessed globally.
An example is given as following below
It will print 100 for inside x because the value of x = 100 is defined inside the
fun, so no error.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
17. It will not print for the outside x and will raise a NameError: that name ‘x’ is
not defined.
Note that when you define both x inside and outside of the function, then it
will prioritize local x over global. It means the local variable has a higher
priority to access than the global variable if both local and global exist. An
example is given below.
Pass Statement
A function can not be empty, to avoid any errors we use ‘pass’ statement.
Can be used as a placeholder as well.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com