top of page

Python Functions: Are Like Gift Boxes

Updated: Aug 8

Python functions are like tools that help us do many things in Python.


Functions are like tools that help us do many things in Python. We can think of them as gift boxes that we — or someone else — have prepared, with everything we need already packed inside. When we open the box (use a function), we can make use of whatever is inside.


Let us say you want to create a program to add two numbers and show the result.

a = 1
b = 2  

a + b

Python functions are like gift boxes
Python functions are like gift boxes!

Now doing this for different values of a and b would require writing the same code again and again. There is a solution! Python allows you to create your own functions and reuse them as many times as needed in your program.


We use the def keyword to define our own functions. Say, we wish to name our function as add, and it will take two numbers, a and b. We can write in Python:

def add(a, b):
	...

We just defined our own function add using def. The function name is followed by parentheses (for values) and a colon. Whatever this function will do is written indented below the first line. For now, we leave it as three dots.


Thus, we can make new functions on our own that can be used in the program. This type of programming is also called modular programming as we are making different modules inside our program. The main features of functions are:


  • They can be reused

  • Easy to organise and maintain

  • We don’t need to write the same code for a function repeatedly


Quick Links:



#1: Python def


In general, you can define a function as:

def function_name():
	...

def your_function_name():
	...

def function_name(): is sometimes also called a function header. The variables inside the parentheses () are called parameters — in our case, a and b. Parameters are optional. Our add function currently does nothing. Let us activate it:

def add(a, b):
	return a + b

We wrote a new line: return a + b indented.

This line:

  • takes the variables a and b

  • adds them

  • returns the result


Whatever is indented inside the function header is the function body. The body may or may not return anything. Our add function is now completed. But it will not show you any results until you use it in your program. Now, how to use or call our function?


Write and run:

add(3, 4)

7


Run your code in this notebook.


#2: Python Function Call


add(3, 4)

This is called a function call. You simply write the name of your defined function followed by parentheses (). Since this function adds two values, you need to provide, or pass, them into the function. Here, 3 and 4 are arguments being passed into the add function. Python will run the code written under def add(a, b): when we call it. The value 3 is assigned to a and 4 to b, and their sum (a + b → 3 + 4) equals 7, which is then returned.


Notice that we don’t need to use the def keyword or a colon : here — our add function has already been defined. Multiple arguments and parameters are separated by commas, and many people use those two terms interchangeably.


Try running:

add(10, 20) 
add(100, -1)

You can now use your add function as many times as you like.


Care: Before calling a function, it should have been defined somewhere in your program. If you call a function before making it, then Python will give you errors.

Exercise

  1. Make a function that greets someone using their name.

  2. Make a function that multiplies any two numbers.


#3: Parameters and Arguments


Parameters are like empty boxes defined when creating a function — they act as placeholders. Arguments are the actual values we put into those boxes when we call the function.


For example:

def add(a, b):
    ...

Here, a and b are parameters (placeholders).

add(3, 4)

Here, 3 and 4 are arguments (actual values being passed into the function).


Be careful when passing arguments into a function — Python follows the order in which parameters are defined. These are known as positional arguments.


If you don’t provide the required arguments when calling a function, you’ll get an error like:

TypeError: add() missing 2 required positional arguments: 'a' and 'b'

To avoid this, you can assign default values to parameters when defining the function:

def add(a=0, b=0):
    return a + b

Here, both parameters have been given default values of 0. This allows you to call the function without passing any arguments — Python will simply use the defaults. This style is also referred to as using keyword arguments, because the parameters are written in a key=value format.

add()
# Output: 0

So, if no arguments are passed when calling a function, Python will use the default values to execute it.


Try this:

add(4)

What do you think the result will be?


Returning Multiple Values


A function can return more values that are separated by commas. We will continue with our example. Let us say we want to find the multiplication of a and b and return it as well.

def add_and_multiply(a, b):
    return a + b, a * b

This way, the function returns multiple values as a tuple.


Run add_and_multiply(3, 4). You will get:

(7, 12)

This means the function returns the sum 7 and the multiplication 12 as a tuple.


We discussed a lot about functions that enable us to write one piece of code for a purpose and reuse it anywhere in our program. Commonly used functions like print(), input(), and len() are built into the Python standard library and are called in-built functions. We use them by calling them and passing the required values.


In the next post, we will learn about tuples.



Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating

Most Read

AI Nine O Subscription

Subscribe to AI Nine O for 90-second learning.

Your question...

Venus will reply here...

VenusMoon Logo

Promoting equitable education for all through the fruitful use of AI.

© 2024-25 by VenusMoon Education | Udyam Registration Number: UDYAM-MP-10-0030480

bottom of page