LightMySky
به انگلیسی

Defining a Function with Parameters

Giving a named block of code a job of its own and passing values into it, so the same steps run on different data.

No account needed. Progress saves in this browser.

What a learner can do afterwards

  • Define a function and call it more than once with different arguments
  • Decide which values the function needs as parameters
  • Replace a repeated block with a single call

1 · Read

A function, also called a procedure here, puts reusable statements under one name. In Python, def starts its definition. Defining it does not run its body; calling it does. For example, singBingo prints B, I, N, G and O once per call. Functions may print text, return a value to the caller, or do both. Reusing a definition does not remove the work performed by each call.

Try it together

Parameters name inputs; arguments supply their values. In sayHello(person), the body print("Hello", person) uses the caller's person value. ```python def offset(value): return value + 4 result = offset(3) ``` return sends 7 back to this caller; it does not print 7. A later call can supply another value.

sayHello("Ira")person = "Ira"prints Hello Ira
This call fills person with the argument used by the greeting body.

reportGrade(studentName, numCorrect) prints a name and a score: (numCorrect / 30) * 100. For Jo with 27 correct, the score is 90. studentName needs text; numCorrect needs a number. With positional arguments, the first value fills studentName and the second fills numCorrect. Reversing these values gives the body values it cannot use as intended.

The full header def pair(left, right): declares two parameters. Both are required; there are no optional parameters or parameters that collect extra arguments. An ordinary positional call therefore needs exactly two arguments. Python rejects a call with one or three before running this function's body. Other definitions can deliberately accept optional or additional arguments.

To replace four identical greeting blocks, define their steps once and put a call at each old location. If a value varies, make that value a parameter. Keep every required use of the greeting. Editing the shared definition then changes what later calls do. Pick a name that describes the job.

Put repeated steps in a named procedure. Use parameters to pass inputs for each call, and call the procedure wherever you need it.

Procedure and parameter teaching adapted from Khan Academy's Defining a procedure and Procedures with parameters, with independently written Python explanations and assessment repairs.

Two readings adapted from Khan Academy's Defining a procedure and Procedures with parameters. Choosing the inputs for one job was independently written for this dataset.

2 · Watch

به انگلیسی

Take it off screen

Print a worksheetA4 with an answer key page for grown-ups. No screen, no internet.

Where it sits

Then practise

8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.

Spotted a problem on this page? Tell us
Defining a Function with Parameters · Computing, ages 15 to 16 · LightMySky