Home ->Getting Started -> Functions -> Functions Programming


EXAMPLES ON MY SAGE PAGE: Functions in Sage


Here we are looking at Functions Programming . They are often called subroutines or procedures. (See also: Functions Math and Commands.)

Definition: A programming function is a named block of code that can be executed one or more times by being called from other parts of the program. Programming functions can have objects passed to them from the calling code and they can also return objects back to the calling code. It is customary to group all of your programming functions at the beginning of your program. Frequently these functions are algorithms or recipes that perform specific numerical mathematics techniques and so there is ready-to-use code for them in the literature.

*Warning: In Sage blocks of code are indicated by indentation, which must match up exactly. Reference For example, the following code yields a Syntax Error because the "return a" statement is not indented in line with function code. See below how it should be!

def random_between_bad(j, k) :
a=int( random()*(k-j+1) ) + j
return a

  • Example 1

    # This programming function accepts two integers j and k and then generates and returns to the calling code a random integer a between j and k (inclusive).
    # It is the users responsibility to make sure that he passes integers j<k.
    # This program uses the built-in sage math functions random() that generates a random decimal in [0,1) and int(x) that yields the integer portion of the number x.
    # All lines of code of your programming function must be properly indented*

    def random_between(j, k) :
    a=int( random()*(k-j+1) ) + j
    return a

    Disection:

    named block of code, i.e. function name is: random_between
    objects passed from calling code to function: two parameters j and k are passed to the function.
    objects returned back to calling code: one parameter a is passed back.

    Use:

    At top of sage page, open a code block and enter and then evaluate the following code. If all is good, there will be no result.

    def random_between(j, k) :
    a=int( random()*(k-j+1) ) + j
    return a

    Now open another code block and enter and then evaluate the following code. If all is good, a random number between 1 and 100 will be printed. It should change with each evaluation of this block.

    p=random_between(1,100)
    print p

    Now open yet another code block and enter and then evaluate the following code. If all is good, a list of 10 random numbers between 1 and 100 will be printed. It should change with each evaluation of this block.

    m=1; n=100;
    list1=[ random_between(m,n) for j in range(10) ]
    print list1

    Notice that we used j both in the function and as the counter. I can never remember how Sage determines which variables are local and which are global so I rarely do this. :)


If your function returns more than one parameter, you assign two variables at the beginning when you call it. There is an example in My Sage Page.

Reference: Sage_for_Newbies, pp.43 (Ted Kosan)


*Warning: Blocks of code are not indicated by curly braces or begin and end blocks as in many other languages. Instead, blocks of code are indicated by indentation, which must match up exactly. Reference
For example, the following code yields a Syntax Error because the return a statement is not indented in line with function code.

def random_between_bad(j, k) :
a=int( random()*(k-j+1) ) + j
return a

Keywords: sage, functions, subroutine, procedure, math, variables, control, repeating