Functions

In programming, a function is a named piece of code that does a specific job. Instead of writing the same instructions over and over, you simply call the function by name — and it handles everything.

In this lesson, you’ll use functions to place a 3D sphere in space and control its size, shape, and position.

Open In Jupyter K-12

Your First Function Call

Run the cell below. A sphere appears in a 3D scene, similar to the one that you saw in the variables notebook.

Hint: Hold the left mouse button to rotate the scene. Use the scroll wheel to zoom.

import scene3d

scene = scene3d.Scene()
scene.set_ground(length=10, width=10)

sphere = scene3d.Shapes.Sphere(diameter=1, segments=16)
sphere.set_position(0, 0.5, 0)
scene.add(sphere)

What Just Happened?

Look at this line from the code above:

sphere = scene3d.Shapes.Sphere(diameter=1, segments=16)

You just called a function. Here’s how to read it:

Part What it means
Sphere The function’s name — it builds a sphere
( ) Parentheses — they tell Python to run the function
diameter=1 A parameter — controls how big the sphere is
segments=16 Another parameter — controls how smooth it looks

A parameter is a value you pass into a function to customize what it does. Change a parameter, and you get a different result.

{ “question_type”: “multiple_choice”, “question”: “What tells Python that you are calling a function?”, “options”: [ { “key”: “a”, “text”: “An equals sign =” }, { “key”: “b”, “text”: “Parentheses ( )” }, { “key”: “c”, “text”: “A colon :” }, { “key”: “d”, “text”: “Square brackets [ ]” } ], “answer”: “b”, “submitted_answer”: “” }

Changing Parameters Changes the Result

What happens if you make the diameter bigger? Let’s find out. Run the code below — the sphere is now three times as wide as before.

import scene3d

scene = scene3d.Scene()
scene.set_ground(length=10, width=10)

sphere = scene3d.Shapes.Sphere(diameter=3, segments=16)
sphere.set_position(0, 1.5, 0)
scene.add(sphere)

Two Parameters, Two Effects

Each parameter controls something different:

Parameter What it controls Try…
diameter How large the sphere is 0.5 → tiny, 4 → huge
segments How smooth the surface looks 4 → blocky, 32 → very smooth

Try changing segments to 4 in the code above and running it again. A low value makes the sphere look like a faceted gem!

{ “question_type”: “true_false”, “question”: “Changing the diameter parameter makes the sphere a different color.”, “answer”: “False”, “submitted_answer”: “” }

{ “question_type”: “multiple_choice”, “question”: “What does increasing the segments parameter do?”, “options”: [ { “key”: “a”, “text”: “Makes the sphere larger” }, { “key”: “b”, “text”: “Makes the sphere’s surface smoother” }, { “key”: “c”, “text”: “Moves the sphere higher” }, { “key”: “d”, “text”: “Changes the sphere’s color” } ], “answer”: “b”, “submitted_answer”: “” }

Try It Yourself

Use the sliders below to experiment with diameter and segments. Adjust each one and run the cell to see how the sphere changes.

import scene3d

DIAMETER = 1.0 #@param {type:"slider", min:0.5, max:4, step:0.5}
SEGMENTS = 16 #@param {type:"slider", min:4, max:32, step:4}

scene = scene3d.Scene()
scene.set_ground(length=10, width=10)

sphere = scene3d.Shapes.Sphere(diameter=DIAMETER, segments=SEGMENTS)
sphere.set_position(0, DIAMETER / 2, 0)
scene.add(sphere)

Placing the Sphere in Space

So far the sphere has sat at the center of the scene. The set_position(x, y, z) function moves it anywhere in 3D space.

It takes three parameters — one for each direction:

Parameter Direction Positive moves…
x Left / Right Right
y Down / Up Up
z Back / Forward Away from you

Run the code below. The sphere is now floating to the right and above the ground.

import scene3d

scene = scene3d.Scene()
scene.set_ground(length=10, width=10)

sphere = scene3d.Shapes.Sphere(diameter=1, segments=16)
sphere.set_position(3, 2, -2)
scene.add(sphere)

Stretching: Width, Height, and Depth

A sphere is perfectly round — the same in every direction. But set_scale(x, y, z) lets you stretch or squash it independently along each axis:

Parameter Controls
x Width (left to right)
y Height (up and down)
z Depth (front to back)

A value of 1 means normal size. 2 means double, 0.5 means half. Use the sliders below to squash and stretch the sphere in each direction!

import scene3d

WIDTH  = 1.0 #@param {type:"slider", min:0.2, max:3, step:0.2}
HEIGHT = 1.0 #@param {type:"slider", min:0.2, max:3, step:0.2}
DEPTH  = 1.0 #@param {type:"slider", min:0.2, max:3, step:0.2}

scene = scene3d.Scene()
scene.set_ground(length=10, width=10)

sphere = scene3d.Shapes.Sphere(diameter=2, segments=24)
sphere.set_position(0, 3, 0)
sphere.set_scale(WIDTH, HEIGHT, DEPTH)
scene.add(sphere)

{ “question_type”: “multiple_choice”, “question”: “In set_position(x, y, z), which parameter moves the sphere up and down?”, “options”: [ { “key”: “a”, “text”: “x” }, { “key”: “b”, “text”: “y” }, { “key”: “c”, “text”: “z” }, { “key”: “d”, “text”: “All three equally” } ], “answer”: “b”, “submitted_answer”: “” }

{ “question_type”: “true_false”, “question”: “Calling set_scale(1, 3, 1) makes a sphere that is taller than it is wide.”, “answer”: “True”, “submitted_answer”: “” }

{ “question_type”: “freeform”, “question”: “What is the name of the parameter that controls the size of a Sphere?”, “answer”: “diameter”, “submitted_answer”: “” }

In your own words, explain what a function is and what a parameter does.

Can you think of a real-life example where you give instructions with specific details — like ordering food with options? What would the function name be? What would the parameters be?