Table of Contents > Programming in Jython > Functions

Functions

Functions are like small, individually named recipes that can be used within a larger recipe (a program). People write whole libraries of functions that can be used to write specific kinds of recipes. Some functions are built right into a programming language because they are used so frequently; others need to be written by you. You can write a function to do pretty much anything you want.


Built-in Functions

Some functions are used so often by programmers that they are built into Jython already. (Other functions have been included with JES to help you write media computation programs. These functions let you do things with pictures, sound, and video. They are listed in the help sections for each media type.)

An example of a function that is built into Jython is the function for finding the square root of a number. The name of this function is sqrt(). To use this function, you need to tell Jython what number you want to find the square root of, and Jython will return the square root of that number.

For example:

>>> sqrt(25)
5.0

(Notice that the square root is returned as a float. See the help section on data types for more information about floats.)

 

jump to top
Range

One important built-in function is the range function. This function allows you to define a sequence of whole numbers by defining the first and last numbers.

For example, if you wanted range1 to represent the sequence 4,5,6,7,8,9,10,11,12, you could do this by typing:

>>> range1 =  range(4,13)
(Notice that the range starts with the first number you type, but ends before the second number.)
jump to top

Original Functions

It's very likely that you will want to write a function of your own at some point. To do this, you simply use the format for writing and naming a program.

For example, the following function could be written to add together two numbers:

def addTwoNumbers(number1, number2):
  total = number1 + number 2
  print total

The first word, def, tells Jython that we are about to define a function or program. The second word, addTwoNumbers, indicates what we want to name this particular function. The things in parentheses indicates that when we use the function addTwoNumbers, we should always include two things to be added! The words inside the parentheses, number1 and number2, are technically referred to as "parameters." Everything after the colon is the body of the function, or the part that tells the computer what to do whenever the function is called: add the two numbers and print the result. (Notice that the body of the function is indented!)

If we write this function and then try to use it without including the two numbers to be added, we will get an error message because we specifically told Jython to expect two numbers. (Actually, we just told Jython to expect two somethings. If we pass it two strings, it will add those too!)

To call the function we wrote above, we simply use the function's name.

For example:

>>>addTwoNumbers(4,6)
10
jump to top