Defining and using functions in Python |
Challenges |
Creating a function
Functions are used to group useful pieces of code together so that you can give them a name and easily use them whenever you want.
Use the keyword def to define a function Give the function a name Add any parameters between the brackets for example ... def square(): This function doesn't have any parameters so we just use round brackets with nothing inbetween - they're still needed though.
|
Using or 'calling' a function
Using a parameter with a function
Calling a function with a parameter
Random numbers and challenges |
The program on the left shows how you can call the same function with a different parameter to make the function do different things.
You should always call a function with exactly the number of parameters set out in the def statement (the definition) |