Programming Using Parameters

f you’ve not read the introduction pages for Procedures and Functions, then now is a good time to go back and read those first!

programming with parameters

Parameters are a cross-over topic between GCSE and A Level. Even though you are looking at them early on in your programming journey parameters will stay with you forever.

They don’t necessarily get harder, but as you learn more about the different styles of programming you will use them in different and more elegant ways.

In their most basic form, parameters are pieces of data that you can pass into a Subroutine. Think back to the validation that you read through on the Functions page – that could be improved by using a parameter. How? Well, the function assumes that you always want to check that they are using the same three options so how could we change that?

Here’s the original pseudocode:

FUNCTION checkUser()
    allowedMenu = [‘Deutsch’, “English”, “Espanol”]
    seeMenu()
    choice = INPUT

    WHILE choice is NOT IN allowedMenu DO
          OUTPUT “Not a Valid Choice”
          choice = INPUT
    END WHILE

RETURN choice

END FUNCTION

 

And here’s the code with a parameter:

FUNCTION checkUser(allowedMenu)
    seeMenu()
    choice = INPUT

    WHILE choice is NOT IN allowedMenu DO
          OUTPUT “Not a Valid Choice”
          choice = INPUT
     END WHILE

RETURN choice

END FUNCTION

 

Notice that we’ve moved the 1st line out and just put the name of the variable into the brackets? This means that we could call the function using different options like this:

Menu1 = [‘Deutsch’, “English”, “Espanol”]

Menu2 =[“Fi”,”Fo”,”Fum”]

checkUser(Menu1)

checkUser(Menu2)

 

By doing this, we can now use that whole section of code to check any user input that has to be within a list of options.

Using the Python code that we created in the previous section, we can improve the efficiency by passing in the allowed options as a parameter. In the example code below, the allowed options are passed into the function allowing the function to be called again with different options without needing to write the code for validation again.

Try expanding the code so that the function can be called again, but this time using a different array of options. To run the program in the browser, press Ctrl + Enter.

Found this page helpful? Please consider sharing!

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on reddit
Share on whatsapp

Looking for More?

Lesson Plan

Presentation

Homework

Revision

Not a member yet? Sign Up or Log In below