Function definitions

Function definitionsΒΆ

You can define your own functions. Functions are introduced using the keyword def. You then need to provide

  • a name of your function

  • the arguments (just like variables)

  • the body of the function (a code fragment to calculate what you want with the arguments)

  • a return statement to indicate the value that the function has calculated given the arguments.

Note

Functions in Python are a little bit more general than functions in mathematics. They can be used to define commands. In this case they might not calculate a value, just have an effect. In this case there is no need for a return statement.

The book Introduction to programming in Python presents function definitions in chapter 2.1