Building Functions in Python

Functions are key concepts in python programming and knowing how to properly interpret and build a function is crucial for beginner python programmers. This post is a step by step tutorial for coding functions in python.

What are functions?

Functions are pieces of code that take an input, run specified code/logic and return an output. Basically, functions help replace sections of code that you may frequently use and condense it to a single line. The figure below demonstrates the basic sections of a function.

Fig 1 Basic Function Structure

The “def” defines the function, “function_name” names the function, “input_items” is the argument/input (the label is determined by the user), “output” is the function body/variable that contains the logic we want the function to perform, “return” will return the value of the function.

Below is an example using the above function.

The function took the string/word “Happy”, added the string/phrase “ Gummy Bears” and returned “Happy Gummy Bears”.

This is the foundation to building functions. Depending on what output a user wants will depend on how complex the function is.

Like what you read? Give David Ortiz a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.