-4

I found this in a program. What does it mean?

uint8_t x(uint8_t, uint8_t, uint8_t)

I'm new to C/C++ and I'm poking around the net to see how others are doing things. I'm sure there's a way to google the answer, but I don't know how to ask the question.

David G
  • 94,763
  • 41
  • 167
  • 253
joelindo
  • 11
  • 3
  • 3
    Looks like a function signature. – πάντα ῥεῖ Dec 27 '22 at 18:13
  • Does this answer your question? [Whats the difference between UInt8 and uint8\_t](https://stackoverflow.com/questions/16937459/whats-the-difference-between-uint8-and-uint8-t) – Nikhil Dec 27 '22 at 18:14
  • 1
    This looks like a function signature with a very bad choice of naming. It's a good idea to provide descriptive naming to your variables, functions ... as you may have to debug your code 3 years from when you wrote it or worse someone else may have to debug it long after you left the company. – drescherjm Dec 27 '22 at 18:20

1 Answers1

2

Given that uint8_t is a type, this looks like a function prototype for a function x which takes three uint8_t arguments and returns a uint8_t value.

Actually implementing said function, the arguments would need to be given names:

uint8_t x(uint8_t a, uint8_t b, uint8_t c) {
    // something
}

You will see prototypes very commonly in header files, though they may be used in simpler programs for teaching purposes. Often to place the main function as early in the code as possible. Functions should be "black boxes" with predictable behavior such that knowledge of their implementation isn't necessary to use them.

E.g.

#include <stdio.h>

void hello(char *name);

int main(void) {
    hello("Bob");

    return 0;
}

void hello(char *name) {
    printf("Hello, %s!\n", name);
}

Though naming parameters/arguments in function prototypes is not required, giving them meaningful names (that match in the implementation) is a way to convey what information they are providing to the function.

Chris
  • 26,361
  • 5
  • 21
  • 42
  • 1
    ... and, for my money, they should be given the same names in the function prototype. – Paul Sanders Dec 27 '22 at 18:20
  • Here's where I got it. https://github.com/drcpattison/ICM-20948/blob/master/src/ICM20948.h line 219 – joelindo Dec 27 '22 at 18:22
  • 2
    @joelindo Those are *member function declarations*. Honestly you are not going to learn much by just looking at other peoples code. Apart from the obvious problem that you may not understand what you are looking at, you also have no way to judge the quality of what you are looking at. – john Dec 27 '22 at 18:34
  • @joelindo That's very clearly a function prototype. [Edit: it's actually a member function declaration, as noted above.] The function is named `writeByteWire` (so it does have a sensible name), it takes three `uint8_t` parameters and returns a `uint8_t`. – printf Dec 27 '22 at 18:49
  • Understood. Roger that! Takes me back to high school: "Yes Mom, I'm doing my homework." Don't get me wrong. I am VERY appreciative of your (and everyone else's) help. – joelindo Dec 27 '22 at 18:51