Professional Documents
Culture Documents
com/basics/python-functions-cheat-sheet
https://www.guru99.com/python-tutorials.html
https://www.tutorialaicsip.com/cs-xii/python-functions-class12/#:~:text=Python
%20functions%20class12-%20Returning%20values%20from%20function
%20A,not%20return%20a%20values%20is%20called%20void%20function.
2. Improves Readability: By using functions for frequent tasks you make your
code structured and readable. It would be easier for anyone to look at the code
and be able to understand the flow and purpose of the code.
3. Avoid redundancy: When you no longer repeat the same lines of code
throughout the code and use functions in places of those, you actually avoiding
the redundancy that you may have created by not using functions.
def function_name(function_parameters):
function_body # Set of Python statements
return # optional return statement
Calling the function:
300
17
Default arguments in Function
Now that we know how to declare and call a function, lets see how can we use
the default arguments. By using default arguments we can avoid the errors that
may arise while calling a function without passing all the parameters. Lets take
an example to understand this:
In this example we have provided the default argument for the second
parameter, this default argument would be used when we do not provide the
second parameter while calling this function.
300
9
101
Types of functions
There are two types of functions in Python:
1. Built-in functions: These functions are predefined in Python and we need not
to declare these functions before calling them. We can freely invoke them as and
when needed.
2. User defined functions: The functions which we create in our code are user-
defined functions. The add() function that we have created in above examples is
a user-defined function.
We will cover more about these function types in the separate guides
When you started coding in Python, you'd have used the built-
in print() function in your Hello World! program 😀 and
the input() function to read in input from the user.
So long as you know how to use these functions, you don't have
to worry about how they've been implemented.
In programming, this is called abstraction. It lets you use
functions by calling the function with required arguments,
without having to worry about how they actually work.
There's a whole wealth of built-in functions in Python. In this
post, we shall see how we can define and use our own
functions. Let's get started!
def function_name(parameters):
# What the function does goes here
return result
You need to use the def keyword, give your function a name,
followed by a pair of parentheses, and end the line with a colon (:).
If your function takes arguments, the names of the arguments
(parameters) are mentioned inside the opening and closing
parentheses.
Please note that in function definition, the arguments that your
function consumes are referred to as parameters.
When you call the function with specific values for these parameters,
they're called arguments or actual parameters. This is because
the arguments in the function call are the values used for the function's
parameters.
Then, you begin an indented block. This is the body of the function
that describes what your function does.
There's a return statement that returns the result of the operation on
the arguments. The return statement returns control to the point
where the function was originally called.
Note that the arguments and the return statement are optional.
This means that you could have a function that takes in no
arguments, and returns nothing. 😀
Let's now try to understand the above statements using simple
examples.
def my_func():
print("Hello! Hope you're doing well")
# Output
Hello! Hope you're doing well
# Output
Hello Jane! Are you from Paris?
What happens if you specify the place first and then the name?
Let's find out.
my_func("Hawaii","Robert")
# Output
Hello Hawaii! Are you from Robert?
We get Hello Hawaii! Are you from Robert? – and this doesn't
make sense. 🙂What's causing this problem?
The arguments in the function call are positional arguments. This
means that the first argument in the function call is used as the
value of the first parameter (name) and the second argument in
the function call is used as the value of the second parameter
( place )
See the code snippet below. Instead of specifying only the
arguments, we've mentioned the parameters and the values
they take.
my_func(place="Hawaii",name="Robert")
# Output
Hello Robert! Are you from Hawaii?
These are called keyword arguments. The order of arguments in
the function call does not matter so long as the names of the
parameters are correct.
How to Create a Function with Default Arguments in
Python
What if we had certain parameters that take a specific value
most of the time during the function calls?
Can we not do better than calling the function with the same
value for a particular parameter?
def total_calc(bill_amount,tip_perc=10):
total = bill_amount*(1 + 0.01*tip_perc)
total = round(total,2)
print(f"Please pay ${total}")
Let's now call the function in a few different ways. The code
snippet below shows the following:
total_calc(150)
# Output
Please pay $165.0
total_calc(200,15)
# Output
Please pay $230.0
total_calc(167,7.5)
# Output
Please pay $179.53
# Output
Volume of the cuboid is 660.0 cubic units
# Output
(512, 384)
Now, we shall unpack the tuple and store the values in two
different variables.
volume, area = cube(6.5)
print(f"Volume of the cube is {volume} cubic units and the total surface area
is {area} sq. units")
# Outputs
Volume of the cube is 274.625 cubic units and the total surface area is 253.5
sq. units