deutsch     english    français     Print

 

3.4 FUNCTIONS WITH A RETURN VALUE

 

 

INTRODUCTION

 

You already know how to define a function with or without parameters and how to call it. From mathematics you probably know that there, functions are understood as something slightly different. In mathematics, a function y = f(x) has an independent variable x. For each value of x, the function returns a value of the dependent variable y. One example is the quadratic function:
.  x = 0, 1, 2, 3 results in the square numbers 0, 1, 4, 9.

You can also define functions in Python that calculate a value and then "return" it as a variable.

PROGRAMMING CONCEPTS: Return value of a function, discretization

 

 

THE KEYWORD RETURN

 

You can define a function squarenumber(a) that calculates a square number a * a for a given parameter a, just as in mathematics. The return occurs by using the keyword return. You can then draw the graph of the function in a GPanel. For the graphics you best use draw(x, y), which draws a line segment from the last position of the graphics cursor to (x, y) and then places it there. After the GPanel window appears, the graphics cursor is at (0, 0). You must first set it to the starting point of the image using move(), otherwise you will get an incorrect starting line.

 

 
from gpanel import *
makeGPanel(-25, 25, -25, 25)

def squarenumber(a):
    b = a * a
    return b

for x in range(-5, 6):
    y = squarenumber(x)
    if x == -5:
        move(x, y)
    else:
        draw(x, y)
Highlight program code (Ctrl+C copy, Ctrl+V paste)

 

 

MEMO

 

With return, a function can return a value to the caller and stop further processing. Just as in mathematics, a function cannot return multiple values [more... return is usually the last statement in the functions block. But this is not necessary.
If return is invoked before, so the function returns at this point and the next instruction is not executed.
].

However, as you have seen, in contrast to mathematics, there are also functions in computer science that do not return a value but can still have an effect. Functions are even able to do both, cause something and return something [more... When you call such a function, the returned value must not be "picked up", you can therefore use a function with a return value as a command].

This graphical representation of the quadratic function is not very nice yet. In addition to the missing coordinate system, the graph's angular progression is also quite unpleasant. This is due to the fact that you only calculate the function at a few integer points that you connect with straight lines. This exposes an essential weakness of computer science compared to mathematics: Although the function delivers a y value for every value of the x-axis (for every real number), in computer science we can only calculate it at a finite number of points. We say that the continuous x-axis is dissolved into discrete points.

 

 

DECIMAL NUMBERS (FLOATS)

 

At least we can make the representation a bit nicer if we choose to calculate points that are close to each other on the x-axis. For example, you can run through the range of -5 to 5 in hundredth steps. To make it even better, you can draw a coordinate grid.

Unfortunately, you can only run for loops with integer values in Python. If you need a better resolution, you can use a while loop. This way, we can add 0.01 to the x coordinate at every step. Now Python does no longer consider x as an integer, but as a decimal number (float).

 

 


from gpanel import *
makeGPanel(-6, 6, -3, 33)
setColor("gray")
drawGrid(-5, 5, 0, 30)

def squarenumber(a):
    b = a * a
    return b

setColor("blue")
lineWidth(2)
x  = -5
while x < 5:
    y = squarenumber(x)
    if x == -5:
        move(x, y)
    else:
        draw(x, y)
    x = x + 0.01
Highlight program code (Ctrl+C copy, Ctrl+V paste)

 

 

MEMO

 

In Python, decimal numbers are called float. Unlike in mathematics, decimal numbers in computer science always have a certain (finite) number of digits. In Python there are about 14 digits (in other programming languages such numbers are called double). One example in computer programming is that you can never exactly specify the number π (which is of course an infinite decimal fraction), because you would only get a precision of around 14 digits with a float.

If you need a coordinate grid, you can do the following:

  • Expand/Enlarge the coordinate range left and right, and up and down by 10% (instead of -5 to 5 use -6 to 6, instead of 0 to 30 use -3 to 33)
  • Call drawGrid() with 4 parameter values, matching the coordinate range that you are actually using. This results in 10 coordinate fields.

 

 

EXERCISES

 
1.

Define the function mean(a, b) that returns the arithmetic mean of the two parameters. Test it out with the console.


2.


Examine the behavior of the function y = cos(x). How is it different from y = sin(x)?


3.


Display the graph of the function y = sin(5x) in a GPanel, for a range between 0 and 2π with a resolution of 0.01 (you can get π using math.pi). Pick a value other than 5 within the sine function. What is the connection between this number and the graph?


4.


Define the function f(x) = 1 / sin(x) and represent it in a GPanel for the range -5...5 (for both axes) with a solution of 0.001. Also draw the coordinate axes with a different color. Do you find anything interesting about this?