Functions: the Python’s Way

How to function works in Python

Emmanuel Jafaru
3 min readDec 31, 2023
Functions in Python

One of the biggest syntaxes used in any programming language is Functions. And Python does not err in that regard.

Functions definition are very important part of writing Python. In this article, You will learn the concept of function in Python, and how to define and call a function in Python.

Defining Functions

There are a ton of built-in functions in Python. That is a very great, if not one of the greatest invention in Python. Regardless, you cannot sufficiently build a program using built-in functions alone.

Hence, a need to understand how to declare and call your specific function in Python.

The syntax for defining a function in Python is written below;

To define a function, use the “def” keyword, then the name of your function, then a colon. Recall Python uses indentation. This means that the body of this code must be inside the ‘def’ keyword, as shown below. When the function is complete, exit the indentation.

#definig a my function
def my_function:
print('I have defined my Function')

The my_function above is a very basic function that simply outputs the string “I have defined my own Function”

In the function above, we are only defining “my_function”. To call the function, we’d have to write out the function with a parenthesis after its name.

#let's call my_function
my_function()

This is the case for basic functions. But many times in real-life scenarios, we want our functions to perform a specific task. To do this, our function will have to accept some parameter(s) to enable it to run the required task at hand.

Parameter(s) and Argument(s): They are variables or data required by a function to run or execute. Parameter(s) are the variables passed into a function when defining the function, while Argument(s) are the variables passed into the function when calling the function.

Case study

We want our function to greet someone.

To do this, we’d have some arguments in our function called name. Then write the function to greet the name we have defined. Let’s walk through this together.

#a function to greet anyone
def greet(name):
print("Greetings ", name)

#call the greet function with my name
greet(Emmanuel)

#showing the output in docstring
"""output will be:
Greetings Emmanuel
"""

This code block defines a function called greet, requires an argument called name to send it’s greetings to.

In the function above, we are passing the argument name in and allowing the name to be passed when called.

Currently, if our function is called without a name passed to it, it will output only the word “Greetings”, but we don’t want that, we want our function to always mention a name when greeting

One interesting thing we can do is to set a default name for the argument, that way our function will not break. If a name is not passed, it will revert to our default name and send our greetings to that name.

#a function to greet anyone
def greet(name = "Emmanuel"):
print("Greetings ", name)

#call the greet function with my name
#output 1
greet()

#output 2
greet(John)

#showing the output in docstring
#output 1
"""output will be:
Greetings Emmanuel
"""

#output 2
"""output will be:
Greetings John
"""

With this solution, we have created a simple function that receives an argument and executes a “greeting” task.

Recap

Some of the things you have learnt from this article include;
* How to define a function
* How to declare and pass argument in a function
* How to call a function
* Using Docstring

I hope you have a great time working with Python 🐍 🐸.

Until next time, Don’t get bitten

--

--