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.

Find this page helpful? Share the love on your social media mentioning @TeachAllAboutIT and we’ll enter you in our monthly draw to win a gift voucher for any product on the site!

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Share on reddit

More For Members

Lesson Plan

Coming Soon!

Presentation

Homework

Students

Click to revise! Members Only

Download links are visible for logged in subscribers.

Not a student member yet? Sign Up Here from £2.50 per month

Or purchase these resources individually from our resources

Sign Up For Membership Today

Level Price  
Individual Student £2.50 per Month. After your initial payment, your first payment is Free. Select
Whole School £12.50 per Month. Select