DEV Community

Cover image for How to Write Functions In Python
Jafaru Emmanuel
Jafaru Emmanuel

Posted on • Originally published at Medium

How to Write 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 own function in Python.

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

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

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 “mu_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()
Enter fullscreen mode Exit fullscreen mode

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.

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
"""
Enter fullscreen mode Exit fullscreen mode

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

In my_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
"""
Enter fullscreen mode Exit fullscreen mode

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! 🐸

Top comments (0)