S3U

Science / Programming basics / Grade 7 / sc770

Programming Basics: Call a Function

Separate a function definition from a call and trace its returned value.

All worksheets
5 questions0m 0s
Learn the skill

Our pseudocode defines FUNCTION addTwo(x): RETURN x + 2; END. A call supplies an input and receives the returned result. Defining a function does not call it. RETURN passes a value back; SHOW displays a value.

Worked example

SET answer TO addTwo(4) stores 6 in answer. The parameter x receives 4 for that call. Nothing is displayed until a SHOW instruction runs.

The function addTwo accepts x and returns x plus two. Calling addTwo with four returns six. A separate SHOW instruction is needed to display the result.
The function addTwo accepts x and returns x plus two. Calling addTwo with four returns six. A separate SHOW instruction is needed to display the result.
Question 1 What does addTwo(4) return?
Question 2 What does addTwo(0) return?
Question 3 Does defining addTwo automatically call it?
Question 4 After SET answer TO addTwo(4), what is stored in answer?
Question 5 Which instruction displays the stored result?