Συναρτήσεις#

Εισαγωγή#

Οι συναρτήσεις αποτελούν θεμελιώδες στοιχείο του προγραμματισμού C++, καθώς συσκευάζουν τμήματα κώδικα σε επαναχρησιμοποιήσιμα, αποτελεσματικά τμήματα κώδικα, σχεδιασμένα να εκτελούν μια συγκεκριμένη εργασία. Οι συναρτήσεις μπορούν να κληθούν πολλές φορές μέσα σε ένα πρόγραμμα, διευκολύνοντας την οργάνωση του κώδικα και συμβάλλοντας στην αποφυγή επαναλαμβανόμενου κώδικα. Οι συναρτήσεις διευκολύνουν επίσης την αποσφαλμάτωση του κώδικα.

Αυτή η σελίδα καλύπτει τα βασικά στοιχεία χρήσης συναρτήσεων σε C++, όπως:

Δομή και Σύνταξη Συνάρτησης#

Στην C++, οι συναρτήσεις πρέπει να δηλώνονται με τον τύπο επιστροφής, το όνομα και τις παραμέτρους τους. Η βασική σύνταξη είναι:

return_type function_name(parameters) {
  // Code to execute when the function is called
  return result;  // Optional, used to return a value
}

Λειτουργικά Μέρη

Περιγραφή

return_type

The data type that the function returns, some common options are:

  • void — Returns nothing
  • int — Returns an integer value
  • double — Returns a floating-point number
  • bool — Returns true or false
  • char — Returns a single character
  • const char* — Returns a text string
  • user-defined types — Returns a struct or class object

function_name

Το όνομα της συνάρτησης.

parameters

Optional. A comma-separated list of typed parameters written as type name, where type specifies the data type of the parameter and name is the identifier used to access that value inside the function. Parameters receive input values when the function is called.

result

Optional. The value returned by the function. If the function’s return_type is not void, this value must be provided and its type must exactly match the specified return_type.

Σημείωση: Μια συνάρτηση πρέπει πάντα να δηλώνεται πριν την κλήση της, διαφορετικά πρέπει να παρέχετε ένα πρωτότυπο συνάρτησης.

Πρωτότυπα Λειτουργίας#

Στην C++, ο κώδικας διαβάζεται από πάνω προς τα κάτω. Αυτό σημαίνει ότι μια συνάρτηση πρέπει να είναι γνωστή στον μεταγλωττιστή πριν να μπορεί να κληθεί.

A function prototype lets you use a function before writing its full code. This allows you to keep main() near the top of your program while placing function definitions later, where they are easier to organize and read.

Without a prototype, you would have to define every function above main(), which can make projects harder to follow as they grow.

Define the function prototype (outside of int main())

// The function's prototype
void greeting();

Call the function inside int main()

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  greeting();
}

Γράψτε το σώμα της συνάρτησης μετά

void greeting() {
  Brain.Screen.print("Hello!");
}

Ορισμός και κλήση συναρτήσεων#

Functions with No Parameters#

If a function does not require input, you can define it without parameters. Use void as the return type if the function doesn’t return a value.

Define the function (outside of int main())

// Define a function to display a message
void greeting() {
    Brain.Screen.print("Hello!");
}

Call the function inside int main()

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Call the function to display the message
  greeting();
}

Functions with Parameters#

Μπορείτε επίσης να προσθέσετε παραμέτρους σε συναρτήσεις, οι οποίες σας επιτρέπουν να μεταβιβάσετε πληροφορίες για τη χρήση της συνάρτησης.

Note: Text can be passed using C-style strings (const char*) or C++ strings (std::string).

Define the function (outside of int main())

// Define a function with a parameter
void namedGreeting(const char* name) {
    Brain.Screen.print("Hello, %s!", name);
}

Call the function with a parameter inside int main()

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Call the function to display the message
  namedGreeting("Stranger");
}

Επιστρεφόμενες τιμές#

Functions can send data back to the caller using the return keyword. This allows you to capture and use the output in your project.

Define the function (outside of int main())

// Define a function that multiplies numbers by 2
int timesTwo(int number) {
    return number * 2;
}

Call the function with a parameter inside int main()

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Display the return value
  Brain.Screen.print("%d", timesTwo(2));
}

Αναφορά διέλευσης#

Από προεπιλογή, η C++ μεταβιβάζει παραμέτρους συνάρτησης κατά τιμή, που σημαίνει ότι η συνάρτηση λαμβάνει ένα αντίγραφο της αρχικής μεταβλητής. Οι αλλαγές που γίνονται μέσα στη συνάρτηση δεν επηρεάζουν την αρχική τιμή.

Define the function (outside of int main())

// Add 1 to the number
void addOne(int number) {
  number++;
}

Call the function with a parameter inside int main()

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  int value = 5;
  addOne(value);
  // "value" will still be 5
}

To allow a function to modify the original variable, parameters can be passed by reference using the & symbol.

Define the function (outside of int main())

// Add 1 to the number
void addOne(int& number) {
  number++;
}

Call the function with a parameter inside int main()

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  int value = 5;
  addOne(value);
  // "value" will now be 6
}

Η μεταβίβαση μέσω αναφοράς επιτρέπει στις συναρτήσεις να ενημερώνουν μεταβλητές που ορίζονται εκτός της συνάρτησης χωρίς να επιστρέφουν τιμή.

Συναρτήσεις επανάκλησης#

Μια συνάρτηση επανάκλησης είναι μια συνάρτηση που μεταβιβάζεται σε μια άλλη συνάρτηση και καλείται αυτόματα όταν συμβεί ένα συγκεκριμένο συμβάν.

Οι επανακλήσεις χρησιμοποιούνται συνήθως για συμβάντα όπως το πάτημα κουμπιών ή οι ενημερώσεις αισθητήρων.

Αντί να καλέσετε τη συνάρτηση εσείς οι ίδιοι, το έργο την καλεί για εσάς όταν συμβεί το συμβάν.

Define the callback function (outside of int main())

// Create a callback function
void onPressed() {
  Brain.Screen.print("Screen pressed!");
}

Register the callback inside int main()

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Call onPressed whenever the screen is pressed
  Brain.Screen.pressed(onPressed);
}

Callback Signatures#

Κάθε συνάρτηση επανάκλησης πρέπει να ακολουθεί μια συγκεκριμένη υπογραφή, η οποία ορίζει:

  • Ο τύπος επιστροφής

  • Ο αριθμός των παραμέτρων

  • Οι τύποι παραμέτρων

Η απαιτούμενη υπογραφή καθορίζεται από το API. Η συνάρτηση επανάκλησης πρέπει να ταιριάζει ακριβώς με αυτήν την υπογραφή.

// This callback function cannot include any parameters
void callback(void);

// This callback function includes four parameters
void callback(axisType axis, double x, double y, double z);

Τα ονόματα των παραμέτρων είναι περιγραφικά και ενδέχεται να διαφέρουν, αλλά ο τύπος επιστροφής, η σειρά των παραμέτρων και οι τύποι παραμέτρων πρέπει να ταιριάζουν ακριβώς.

Συναρτήσεις με προεπιλεγμένα ορίσματα#

Οι συναρτήσεις μπορούν να ορίσουν προεπιλεγμένες τιμές για μία ή περισσότερες παραμέτρους. Ένα προεπιλεγμένο όρισμα χρησιμοποιείται όταν δεν παρέχεται τιμή για αυτήν την παράμετρο κατά την κλήση της συνάρτησης.

Κανόνες για τα προεπιλεγμένα ορίσματα:

  • Τα προεπιλεγμένα ορίσματα πρέπει να είναι οι τελευταίες παράμετροι στον ορισμό της συνάρτησης.

  • Μόλις μια παράμετρος αποκτήσει μια προεπιλεγμένη τιμή, όλες οι παράμετροι μετά από αυτήν πρέπει επίσης να έχουν προεπιλεγμένες τιμές.

  • Εάν μια συνάρτηση καθορίζει προεπιλεγμένα ορίσματα στο πρωτότυπο, αυτές οι προεπιλεγμένες τιμές δεν πρέπει να καθοριστούν ξανά στον ορισμό της συνάρτησης.

Define the function (outside of int main())

// Required parameter first, default parameter last
void greeting(const char* name, int repeatCount = 1) {

    for (int i = 0; i < repeatCount; i++) {
        Brain.Screen.print("Hello, %s!", name);
        Brain.Screen.newLine();
    }
}

Call the function inside int main()

int main() {
  /* vexcodeInit() is only required when using VEXcode.
  Remove vexcodeInit() if compiling in VS Code. */
  vexcodeInit();

  // Say what name to greet (IQ) and
  // use default value for repeatCount (1)
  greeting("IQ");

  Brain.Screen.newLine();

  // Overrides the default value to 3
  greeting("IQ", 3);
}