How to Create and Call a Function in Python



Python


In this article, we show how to create and call a function in Python.

A function, or method, is a code that tells a program to do something. This can be anything, such as printing out a string, performing a mathematical calculation, etc.

To create a function in Python, the word def precedes the function name and the function name is followed by (), which is then followed by :.

Below, we create a function called gobeach().



So, again, we create a function called gobeach().

This function prints out the statement, "Let's go to the beach" when called.

This brings us to the next thing, how to call a function.

So, if you ran the above code, it wouldn't do anything. The above code simply creates the function, gobeach(), but it doesn't do the function, because it isn't called.

To call a function in Python, the function name followed by () is used.

The following code below calls the gobeach() function.



So you can see now how the function is executed.

And this is all that is necessary to create and call a function in Python.


Related Resources

How to Show All Tables of a MySQL Database in Python

How to Count the Number of Rows in a MySQL Table in Python



HTML Comment Box is loading comments...