Function backets

I am confused about the purpose of the brackets in a function. The more I read, the more confused I'm getting. Will someone please explain in simple terms?

() often called brackets, properly "parentheses" (plural)
- brackets, often called square brackets
{} - braces, never called brackets, its too confusing otherwise!

So I'm not sure what you are asking about...

If its the argument list, this occurs in brackets (parentheses) after the expression
that evaluates to a function (typically this is just the function name).

  foo (1, 2, 3) ;  // call function foo with 3 values...

When declaring a function you put "formal parameters" in the brackets:

int foo (int a, int b, int c)
{
  ...
}

Formal parameters tell the compiler what type the arguments the function are
going to be and provide names for refering to them in the function body.

If you meant braces, not parentheses:

In C and C like languages a statement is one of several possibilties:

if statement
while statement
declaration
assignment
etc etc

but it can also be

{ list-of-statements }

A function body is required to be of this form, although its really just a statement,
there are some technical reasons(*) for disallowing:

int bar (a)
  return a + 3 ;

So you have to say

int bar (a)
{
  return a + 3 ;
}

MarkT:
{} - braces, never called brackets, its too confusing otherwise!

I'm sorry for English not being my mother language.

But I know "braces" as something that some adolescents wear for teeth correction.

And { } are for me just "curly brackets".
:smiley:

So forgive me if I'd use the wrong name for such brackets in my forum messages.

The English also used to use braces to hold up their trousers. The Americans called them suspenders. But the English use the word suspenders for what the Americans call garter-belts so they don't mention them in polite society.

Curly brackets is fine.

...R

I take your point concerning terminology MarkT. As a Printer in my other life I know the correct terms for these things. It makes it harder for a beginner to have to deal with labels that are not too apposite but I suppose that’s the way of things these days.
The reason I wanted to know more about the use of function’s parentheses was to discover if I could put data inside them (say a digitalRead) then run the function which would return with the result of the routine in the same brackets AND how I would access that data.

Such as this?

if (digitalRead(pinX) == LOW){
// do something
}
else {
// do something else; or leave out & just do next command
}

All functions have something called a function signature, like:

int findInches(int yards)    // The entire line is called the function signature
                             // Put a semicolon at the end of the line and it's a function prototype

The keyword int is called the function type specifier. It tells you the type of data this function is designed to return to whomever called the function. FindInches is the function name. Function names follows the same naming rules as variables. The opening parenthesis (the ‘(’ ) marks the start of zero or more function parameters. Everything between the opening and closing parentheses is called the function parameter list. The parameter list may have zero or more parameters. In this example, we have only one parameter, yards.

The complete function is defined by the function body:

int FindInches(int yards)    // The entire line is called the function signature
{                            // <---- Start of function body
   return yards * 12;        //  <---- Function body
}                            // <---- End of function body

Note how the function parameter is needed to let the function return the proper value, which is an int. To use the function, you “call” it, as shown here:

void loop()
{
   int yds = 10;

   int inches = FindInches(yds);  // This calls the FindInches() function
}

Think of it this way: in the last line, you take a piece of paper and write 10 on it, pick up your backpack and stick the paper in it. You then walk to the FindInches() function. As soon as you get there, the door opens, the function grabs your backpack, takes the paper out, reads it, closes the door, does some magic, opens the door and gives you a piece of paper with 120 written on it and you stick it in your backpack. You walk back to loop(), open your backpack and hand the piece of paper to inches, which reads it and makes itself equal to 120. The process of you walking to the function is often called “calling a function” and walking back from the function is called “returning from a function call”. If the function didn’t stuff anything into your backpack, the function type specifier is void. Think about it…

Yes, sort of, Crossroads.

//===========================================================

void resetClock(){
  if (digitalRead(resetClockPin)=HIGH){
  buttonState=digitalRead(resetClockPin);
  deBounce();
    }
  else return;
  
//===========================================================

 void deBounce(){
 

carryon=0;
lastDebounceTime=millis();


do{

if (buttonState != lastButtonState)lastDebounceTime=millis();  

if (buttonState = lastButtonState){
      totalDebounceTime=millis()-lastDebounceTime;
        if (totalDebounceTime>=debounceDelay){
          carryon=1; 
            //startReset;
}
}
lastButtonState=buttonState;

}while (carryon=0);
}

I want to come out of the debounce routine and carry on with the reset if the resetClockPin is HIGH This is where I wondered if I could use the parentheses
I know I'm not being very clear but that is just how my mind is at the moment

Econjack, I'm printing out your words and will have to study them. Thanks.

Need two == in these
if (digitalRead(resetClockPin)=HIGH){
check them all.

here too
}while (carryon=0);

The demo several things at a time has a number of small functions in it. It may illustrate what you are after.

And you could use it as a basis for experiments.

...R

Right econjack, got it!!! You explain things much like the way I used to explain how electronic components work!
Thanks.

econjack:

int FindInches(int yards)    // The entire line is called the function signature

{                            // <---- Start of function body
  return yards * 12;        //  <---- Function body
}                            // <---- End of function body

When I went to school, in the UK, there were 36 inches in a yard.
12 inches in a foot.
3 feet in a yard.
3 X 12 = 36
Maybe its different in the US? :slight_smile:

CrossRoads:
Such as this?

if (digitalRead(pinX) == LOW){
// do something
}
else {
// do something else; or leave out & just do next command
}

Maybe this will clear things.
In general - function can return a value.
In this example -digitalRead() function returns a I/O state / value - either HIGH or LOW.
Than the "if" construct can be looked as a function which evaluates the "if(condition ) " - in this example - read it as "is function digitalRead returning LOW?"

It could be written as (LOW== digitalRead(pinX)) with same results, but it is customary written as (digitalRead(pinX) == LOW).

The example is using function as "condition" in "if" construct and can be further simplified since the (condition) is evaluated for true ( HIGH) or false (LOW).

if( digitalRead(pinX) )
.....

Of course the (condition) can be as complex as required and it will always evaluate to true or false.

if( today is Friday and it is after 1700 and I got money in my pocket )
let's go have a beer ....