Skip to main content

Discrete Math with SageMath: Learn math with open-source software

Section 1.7 Defining Functions

Sage comes with many built-in functions. Math terminology is not always standard, so be sure to read the documentation to learn what these functions do and how to use them. You can also define custom functions yourself. You are welcome to use the custom functions we define in this book. However, since these custom functions are not part of the Sage source code, you will need to copy and paste the functions into your Sage environment. If you try to use a custom function without defining it, you will get a NameError.
To define a custom function in Sage, use the def keyword followed by the function name and the function’s arguments. The function’s body is indented. When you call the function, the return keyword returns a value from the function. The function definition is only stored in memory after you run the cell. You will not see any output when you run the cell that defines the function. You will see output only when you call the function. A green box under the cell indicates the successful execution of the cell. If the box is not green, you must run the cell to define the function.
You may have heard of Pascal’s Triangle, a triangular array of numbers in which each number is the sum of the two numbers directly above it. Here is an example function that returns the \(n^{th}\) (0-indexed) row of Pascal’s Triangle:
Try calling the function for yourself. First, run the Sage cell with the function definition to define the function. If you try to call a function without defining it, you will get a NameError. After defining the function, you can use it in other cells. You won’t see any output when you run the cell that defines the function. The Sage cells store the function definition memory. You will see output only when you call the function. After running the above cell, you can call the pascal_row() function.
Input validation makes functions more robust. We may get some validation out of the box. For example, if we try to call the function using a string or decimal value as input, we will get a TypeError:
However, if we try to call the function with a negative integer, the function will return an empty list without raising an error.
This lack of error handling is risky because it can go undetected and cause unexpected behavior. Let’s add a ValueError to handle negative input:
Running the above cell redefines the function. Try calling the function with a negative integer to see the input validation.
Functions can also include a docstring to provide documentation for the function. The docstring is a string that appears as the first statement in the function body. It describes what the function does and how to use it.
After redefining the function and running the above cell, view the docstring by calling the help() function on the function name. You can also access the docstring with the ? operator.
For more information on code style conventions and writing documentation strings, refer to the General Conventions article from the Sage Developer’s Guide.