Understanding Functions in Python- Wakeupcoders

Python Functions

A function is a segment of clean, reusable code that does a single, connected operation. A higher level of code reuse and improved programme modularity are provided via functions.

As you are well aware, Python has a large number of built-in functions, such as print() and others, but you may also write your own. User-defined functions are what these processes are known as.

Definition of function

Python functions

To offer the necessary functionality, you can define functions. Here are some straight forward Python definition rules.

  1. The word “def” before the function name and parenthesis (()) in a function block.
  2. These parenthesis are where you should put any arguments or input parameters. Within these parenthesis, parameters can also be defined.
  3. The documentation string for the function, often known as the docstring, can be used as an optional initial statement in a function.
  4. Every function has an indented code block that begins with a colon (:).
  5. Returning an expression to the caller is possible when leaving a function using the return statement. Return None is the same as a return statement with no arguments.

How to create a function

The Python def keyword is used to define functions.

def test_function():
print("Hello world")

How to call the function

Function calls

Use the function name in parentheses to invoke the function:

def test_function():
print("Hello world")

What are arguments in function and how we can pass it in the function

Function arguments

Functions accept parameters that can contain data.

The function name is followed by parenthesis that list the arguments. Simply separate each argument with a comma to add as many as you like.

The function in the following example only takes one parameter (fname). A first name is sent to the function when it is called, and it is utilised there to print the whole name example:

def test_function(name):
print(name + "is the name")


Count of arguments

Count of arguments

A function must always be called with the appropriate amount of parameters by default. In other words, if your function requires 2 parameters, you must call it with 2 arguments, not more or fewer.

def test_function(name, place):
print(name + " " + place)

test_function("sam", "delhi")

if you are unsure about the the count of arguments we need to pass we can use args * arbitrary arguments, just pass * before the arguments. for example:

def test_function(*fruits):
print("the recommended fruits are " + fruits[2])

test_function("apple", "banana", "papaya")

How to pass list as an arguments?

Any data type can be sent as an argument to a function (string, integer, list, dictionary, etc.), and the function will treat it as that data type.

For instance, if a List is passed as an argument, it will still be a List when it gets to the function.

def test_function(vegetables):
for i in vegetables:

vegetables = ["onion", "potato", "tomato"]


Return statement

Function return

Use the return statement to allow a function to return a value

def test_function(sum):
return 3 + sum




Python also permits function recursion, allowing specified functions to call one another.

A frequent idea in math and programming is recursion. It denotes that a function makes a call to itself. This has the advantage of allowing you to loop over data to arrive at a conclusion.

The developer should exercise extreme caution when using recursion since it is quite simple to write a function that never ends or consumes excessive amounts of memory or processing resources. Recursion, however, may be a very effective and mathematically beautiful technique to programming when used properly.

Test() is a function that we built in this example to call itself (“recurse”). The data is the i variable, which decreases by one (-1) with each recursion. When the criterion is not bigger than 0, the recursion comes to an end (i.e. when it is 0).

It may take some time for a novice developer to understand how precisely something operates; the best way to do so is to test and alter it.

def test(i):
if(i > 0):
result = i + test(i - 1)
output = 0
return output

print("\n\test output")

Let’s Wind up:

If you liked this Blog and found this blog informative do share with your friends and co-workers to help them. Till then keep Reading and Enjoying.

Contact us

If you are having any feedback or looking a way to connect with us.

Let’s connect together www.wakeupcoders.com/contact & think beyond everything.

Thanks to the Wakeupcoders Team: For thinking beyond everything

Purnima Sinha (Author), Narayan Jha (Editor), Anjali Jha (digital marketing)



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


We make your business smarter and broader through the power of the internet. Researcher | Web developer | Internet of things | AI | www.wakeupcoders.com