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.

More For Members

Lesson Plan

Coming Soon!

Presentation

Coming Soon!

Homework

Coming Soon!

Not a member yet? Sign Up Here