Python Programming - Lesson 3 (Functions)

Words
240
Reading
2 min
Listen
Play
10y

In today's Python Programming Lesson, I will be teaching you how to create and use Functions.

What is a Function?

A functions is a block of organized code that is reusable. They can perform a single action. For example, print() is a function which is built in to Python. We can create our own functions which are known as user-defined functions

How to define a Function

There are some very basic rules in order for you to create a function:

  • Functions are created with the keyword def followed by the name and parentheses (()).
  • The parentheses are used to input parameters or arguments. You place them in the parentheses. You can use a function to define parameters.
  • Each function uses a colon. Your code will then become indented in certain areas.
  • The return statement exits the function.

How to write a Function in Python

We write a function like this:

def functionname(parameters):
"function_docstring" #Indentation
function_suite #Indentation
return [expression] #Indentation

A more visual guide:

Calling a Function

Once you have defined your function, it can be executed using another function or using the Python prompt. I have created some basic source code here:

If you would like more details on how to use functions or anything in Python, feel free to let me know by leaving a comment. If you enjoyed, please UPVOTE and FOLLOW me so that you don't miss any releases of my new content. Thanks for reading,

Jack

Python Programming - Lesson 3 (Functions) | Ecency