简体   繁体   中英

declaring a variable in C++

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.

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.

Eg

#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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM