Our team is trying to generalize the block code (Rookie team 6th grader) but struggling to create function with argument (parameter). When we drag and drop function, it shows us input name. We can add multiple input name (equivalent to argument) and name them but in code it shows code in Java as below where argument type is shown UNKNOWN. My question is: how do we create input variable type?
PS: We have tried creating Variable (In variable also it doesn’t ask type) and then try to give same variable name as input name but that also didn’t work.
Please advise if someone has solved this problem?
private void raiseArm(
// TODO: Enter the type for argument named armDegree
UNKNOWN_TYPE armDegree) {
}
Blocks doesn’t have a concept of “concrete variable types”, which is why looking at the Java code can be quite confusing (please DO NOT take the Java “translation” to heart - the Java translation is just there to be helpful, the Java code being shown IS NEVER ACTUALLY USED EVER, and is sometimes wrong).
All variables in Blocks start out as “typeless”, meaning they will take on the type of the first use - also remember that ALL variables in Blocks are GLOBAL, meaning you do NOT have function-scoped variables. Therefore all variables (including function parameter variables) need to be uniquely named across the entire project if you truly intend for them to be only accessed in specific contexts (and then you just have to remember that’s what you did). For example, here’s a Blocks program where I define a function parameter but I can use that variable from within another blocks function because it’s actually global (and I can change that variable within another function because of its global nature).
This code shows that the value of the parameter is changeable within the function from outside the function because of the global nature of all variables.
Anyway, if you use a “string” with a variable, that variable will take the “string” type and using a variable with a “string” and a “boolean” will balk at one of the types giving you indication you’ve mixed types.
I have one more question on 2M distance sensor. We simply wrote block code to measure distance returned by sensor and manually measure, we found that sensor distance is varying by 2-5 cm. This is causing big problem to be accurate. is this expected or we are doing something wrong?