Defining Functions

Description

To define a new function in Sage with Python, use the def command and a colon after the list of variable names. Notice that after the definition of the function, the code is indented.

Sage Cell

Code

def is_even(n):
   return n%2 == 0
is_even(23)

Options

To define a function in Sage similar to $f(x)=x^2-3$ at $x=1$ we can do the following;

Code

f(x)= x ^ 2 -3
f(1)

Tags

Primary Tags: Programming: The Python language.

Secondary Tags: The Python language: Functions.

Related Cells

None

Attribute

Permalink:

Author: Sage Tutorial v7.6

Date: 07 Jul 2017 15:04

Submitted by: Tom Judson

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License