What is "parameter int d"?

Hi, I'm a beginner that is trying to learn to code, and I am working through the Simon Monk "Programming Arduino..." book and have ran across something that is not explained clearly to me in the "Parameters" section of the Functions chapter.

There is what looks like a variable being declared called int d. I don't see that d was defined anywhere, so I don't understand how a letter can be used as an integer.

I have attached a picture with the areas of question circled in red.

Thanks for any help.

It is the delay, in milliseconds, that make the LED flash on and off. So, a value of 1000 will give 1 second period.

Thanks for the quick reply. How is d used as a time? I don't see d defined as an integer anywhere.

You should always post code in text form, inside a code block.

The function loop() calls the function flash() with the arguments 20 and delayPeriod You can see from the top of the program, delayPeriod = 250

The function flash() receives the second parameter d which is delayPeriod... which you know ...

You mean other than where you circled it in red being defined as an integer?

Thanks for the info on posting code.

So, is d a built-in thing that the IDE knows is referring to the delayPeriod?

Read it harder.

Functions have parameters, your function has one called d that is an int.

When you call the function, you supply the arguments which correspond one-to-one with the parameters, in this case you supply an integer for d, like 42 or an expression that ultimately is seen as an integer.

Simon prolly covers how arguments have some rules and flexibility.

a7

No, that's just what someone chose as an identifier. She might as well have called it broccoli for all the meaning it conveys, that is to say a name other than d might have been better.

a7

Thanks for the quick help folks. I think it is making more sense now.

What you're confused about is functions and function input arguments.

The line

void flash (int numFlashes, int d)

is the declaration of a function named flash, which accepts two input arguments, both of which must be integers. With the function definition on Lines 15–24, your sketch will be able to use expressions such as flash(20, 250) — this will "call" (i.e., execute) the function flash, using the values 20 and 250 as input arguments.

In order for the function definition to perform any actions based on the values of the input arguments, the function declaration must specify variable names representing each input argument (as well as the data type for those variables).

Thus, the expression void flash (int numFlashes, int d) specifies that the variable name numFlashes will be used to refer to the first input argument, and the variable name d will be used to refer to the second input argument (the expression also requires both of the input arguments to be of the type int).

Therefore when the flash function is called using the expression flash(20, 250) , the value of numFlashes is set to 20, and the value of d is set to 250.

1 Like

Just curious. You were confused by the parameter d, but not numFlashes. Would it have been less confusing if instead of d, delayTime was used?

void flash (int numFlashes, int delayTime)
{
    ...
    delay(delayTime);
    ...
}

I think this is a good illustration of one reason why giving variables meaningful names is important. The name could be anything, why not make it mean something?

2 Likes

The lesson: use descriptive variable names so anyone can read your code.

3 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.