Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Parameter

From Groundhog Learning
Revision as of 15:57, 21 October 2025 by Groundhog (talk | contribs) (Updated via bot)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

A value a function can use to performs its tasks.

Details

Functions can take input values to perform tasks in slightly different ways.

Consider the logic to update the score when the player destroys an enemy:

🤖 Pseudocode

add points to score
show score on screen

Let’s say the number of points depends on the type of enemy destroyed:

  • 100 points for stronger enemies;
  • 50 points for weaker enemies;

We could create separate functions for each case:

function strongerEnemyDestroyed:
    add 100 to score
    show score on screen

function weakerEnemyDestroyed:
    add 50 to score
    show score on screen

While this works, it has several drawbacks:

  • Code repetition: It becomes harder to maintain and more prone to errors.
  • Scalability issues: Adding new enemies requires creating more functions.

Instead, we can define a single function with a Parameter - points:

🤖 Pseudocode

function updateScore(points):
    add points to score
    show score on screen

The points parameter, let’s the function accept the number of points as input value.

Now, whenever the player shoots an enemy, we call the function with the number of points.

The values passed in the function call becomes the value of points inside the function:

🤖 Pseudocode

updateScore(100) # for a stronger enemy
updateScore(50) # for a weaker enemy

The actual number of points passed in the function - 100 and 50 - are called arguments.

Why this works well:

  • It performs the same steps but can handle different inputs.
  • It makes the function flexible and reusable.
  • It avoids repeating the same instructions with small changes.

Related Concepts

References