If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

Procedures with parameters

When programming, we use procedures to make our code more reusable and to better organize our code. Often-times, we pass parameters to a procedure so that we can change the output of the code.

The need for parameters

Consider this JavaScript code:
var greeting1 = "Hello, Jackson, how are you?";
var greeting2 = "Hello, Mr. H, how are you?";
var greeting3 = "Hello, Stranger, how are you?";
There's a lot of repetition in that code: it continually stores a string inside a variable that greets someone and then displays it on the screen. However, there is also variance in the code: the name changes each time.
We can create a procedure with parameters to handle both the repetition and the variance.
To specify parameters in JavaScript, we write the name of the parameter (or parameters) inside the parentheses that come after the function name. We then reference that parameter name inside the function.
function sayHello(person) {
  var greeting = "Hello," + person + ", how are you?";
Now we can call that function and pass our desired value for the parameter:
sayHello("Mr. H");
That syntax should look familiar, since we've been calling a function with parameters since the very beginning: println()!
Now we can start using parameters in our own user defined functions and make them much more customizable.
✏️ The program below generates funny names using a function that takes a single parameter. Try calling the function more times and modifying the code inside the function to see what changes.
📝 See similar code in: App Lab | Snap | Python

Multiple parameters

What if one parameter isn't enough to customize our procedure the way we want it? That's no problem, we can just specify more parameters!
In JavaScript, we can specify multiple parameters with a comma-separated list inside the parentheses, making sure to give each parameter a unique name.
In the code below, the reportGrade function accepts two parameters, and uses them to report the score of a student on a 30-question test:
function reportGrade(studentName, numCorrect) {
  var score  = (numCorrect/30) * 100;
  var report = studentName + " earned: " + score;

reportGrade("Sally", 27);
reportGrade("Wilbur", 24);
📝 See similar code in: App Lab | Snap | Python
Whenever you're writing procedures with parameters, pay careful attention to your parameter names, since you need to reference those names exactly inside the procedure. It helps to make the names very descriptive.
✏️ Ever played Mad Libs? The following program uses a procedure with eleven parameters to generate a story. Ask a classmate for parameter values and see what story comes out!
📝 See similar code in: App Lab | Snap | Python

Passing variables as parameters

In the examples above, we've been passing literal strings and number values as the parameters, like "Sally" and 27. We can actually pass any programatic expression that evaluates to a value, like a variable.
This procedure displays the result of multiplying two numbers:
function showMathFact(num1, num2) {
   var multiplied = num1 * num2;
   println(num1 + " x " + num2 + " = " + multiplied);
I can easily show just the multiples of a particular number by storing the first number in a variable, and always passing that as the first parameter:
var firstNum = 9;
showMathFact(firstNum, 1);
showMathFact(firstNum, 2);
showMathFact(firstNum, 3);
✏️ Try changing the value of the firstNum variable in the program below, and notice how that affects every function call below it.
📝 See similar code in: App Lab | Snap | Python

Parameters in pseudocode

This pseudocode represents a procedure that takes two parameters named parameter1 and parameter2:
PROCEDURE name (parameter1, parameter2)
Here's pseudocode for the reportGrade procedure and calls:
PROCEDURE reportGrade (studentName, numCorrect) {
    score ← ( numCorrect / 30 ) * 100
    report ← CONCAT(CONCAT(studentName, " earned: "), score)

reportGrade("Sally", 27)
reportGrade("Wilbur", 24)
🙋🏽🙋🏻‍♀️🙋🏿‍♂️Do you have any questions about this topic? We'd love to answer— just ask in the questions area below!

Want to join the conversation?

  • blobby green style avatar for user 220018
    why did they make this this is harder then its need to be
    (32 votes)
    Default Khan Academy avatar avatar for user
  • leafers seedling style avatar for user Victoria
    I don't understand the difference between the parameter and the variable. Isn't the parameter a variable too?
    (4 votes)
    Default Khan Academy avatar avatar for user
    • hopper jumping style avatar for user pamela ❤
      From the author:Great question! A parameter is a value that is passed into a procedure. Once it is passed into the procedure, it is stored in a local variable in the procedure. So when a procedure has parameters, it means that you can pass values to the procedure that will become variables inside the procedure.

      In terms of passing values, note that you can pass either literal values, variables, or expressions - anything that evaluates to a value eventually:

      Consider these three JS examples:

      showPoint(50, 25);

      var x = 50;
      var y = 25;
      showPoint(x, y);

      var x = 20;
      var y = 25;
      showPoint(x + 30, y);

      They all pass 50 and 25 to the function, and they will become available in the function as variables. However, their names will be whatever they are called in the function header. For example:

      function showPoint(xPos, yPos) {
      ellipse(xPos, yPos);

      If that is how the function is defined, then the values passed in will be stored in the local variables xPos and yPos, regardless of what their variable names were outside of the function.

      Does that make sense? Let me know where I can clarify further.
      (16 votes)
  • piceratops tree style avatar for user Mary San
    What is "programatic" under "Passing Variables as parameters" Is that a typo? Do they mean "pragmatic" or "programmatic?"
    (2 votes)
    Default Khan Academy avatar avatar for user
  • blobby green style avatar for user doudins08
    will a python code procedure that invokes global variables instead of a "parameter" count?
    Something like this lets say:
    def random():
    global num1
    global num 2
    print(random(num1, num2))
    (1 vote)
    Default Khan Academy avatar avatar for user
    • male robot donald style avatar for user ldahodwala
      Using the global syntax like that will let you access those variables outside of the function. However, if you're doing it like that, you should probably reconsider.

      Not to get extremely technical, but modern programming languages are designed around the concept of a "closure". Essentially, variables are always scoped and you can't access data from outside that scope. This scoping allows you to reuse variable names and keep data consistent and secure by limiting which software can manipulate it. While the global syntax exists, I don't find myself using it at all when writing serious code, and if you're using it, I highly recommend you find another way to accomplish your goal.

      But to answer your question, yes. Setting the global variables outside the function, then manipulating those global variables inside the function definition is analogous to using parameters. However, parameters are the preferred choice.
      (2 votes)
  • blobby green style avatar for user ammuzcharia
    do procedures always call for an output? Like will the end result always be returned?
    (1 vote)
    Default Khan Academy avatar avatar for user
  • blobby green style avatar for user tafseerahmad357
    it could have been made this simple.
    function showMathFact(firstNum, num2) {
    var v=9;
    showMathFact(v, 1);
    showMathFact(v, 2);
    showMathFact(v, 3);
    showMathFact(v, 4);
    showMathFact(v, 5);
    showMathFact(v, 6);
    showMathFact(v, 7);
    showMathFact(v, 8);
    showMathFact(v, 9);
    showMathFact(v, 10);
    (0 votes)
    Default Khan Academy avatar avatar for user
  • duskpin ultimate style avatar for user Dakota B.
    how do you do this when it's so hard? (I'm not a programmer!)
    (0 votes)
    Default Khan Academy avatar avatar for user
  • blobby green style avatar for user NAVEED RIAZ
    In the example of sayHello function, don’t v need to define 'person' as a var first?
    (0 votes)
    Default Khan Academy avatar avatar for user
    • old spice man green style avatar for user Jim E
      When we name or specify the parameter in the function
      declaration, it will become a variable in that function or procedure scope.
      function sayHello(person)
      So there is no need to declare it separately. Some languages that have different types needs to have the type also when the parameter is specified.
      For example a function declaration in C:
      int sum(int a, int b);
      (0 votes)
  • leaf orange style avatar for user Chris
    function reportGrade(studentName, numCorrect) {
    var score = (numCorrect/30) * 100;
    var report = studentName + " earned: " + score;

    reportGrade("Sally", 27);
    reportGrade("Wilbur", 24);

    In the var score = (numCorrect/30) * 100;
    why is it showing the number they got correct instead of the
    percentage. If they got 27 correct then it looks like it is dividing 27 by 30 and multiplying it by 100 which, on my calculator gives me 90. Since it looks like it is printing the report which shows the persons name and their score.
    (0 votes)
    Default Khan Academy avatar avatar for user
    • blobby green style avatar for user scottschroeder1993
      reportGrade is showing how you call the function. The result isn't displayed. When you call the function you have to give it the name of the student and how many correct answers (which you can see inside brackets on the first line which reads:

      function reportGrade(studentName, numCorrect)

      If you called the function then it would show the percentage, which resolves to 90 for Sally as you've noticed. Hope this was helpful for someone.
      (0 votes)
  • marcimus orange style avatar for user Renata Kysil
    Hi! I made a Quiz and a procedure to calculate the score (on a different website). The procedure provides a certain amount of points for each correct answer. Is the procedure adding up all the points considered a parameter? In other words, are the points a parameter?
    (0 votes)
    Default Khan Academy avatar avatar for user