Don't provide code dumps. Provide guides that outline the process of solving the problem.
Create a function that squares a number passed to it
Don't just give out a fully-coded solution. The answer to this question is not as simple as regurgitating the syntax. It's a process of breaking the question down into smaller steps and taking those steps on one at a time.
It might start like this:
When you encounter a question like this, you might start by breaking
it down into its individual parts.
- We know we need to create a function.
- We know the function needs to take a numeric argument.
- We know the function should return the square of the number passed into it.
And if the students are writing and running the code, it might continue like this:
Now that we have these steps written down, we can start implementing
them one at a time. Let's start by creating a function:
public void square(){
System.out.println("here");
}
We might run this function to double-check that it's working how we
expected. Next let's add an argument:
public void square(int number){
System.out.println("number: " + number);
}
Finally, let's add a return type to the function and return the square
of the number:
public int square(int number){
return number * number;
}
Or if students are writing their code down on paper, it might look like this:
Start by writing the function definition. We know it needs a numeric
return type and that it should take a numeric argument:
public int square(int number){
Next, we need to create the square of the number:
int numberSquared = number * number;
Finally, we need to return this value:
return numberSquared;
Putting it all together, it looks like this:
public int square(int number){
int numberSquared = number * number;
return numberSquared;
}
Just giving out a full code solution doesn't really help with the actual process of writing code, which is what you're actually teaching.