Συναρτήσεις#
Εισαγωγή#
Οι συναρτήσεις αποτελούν θεμελιώδες στοιχείο του προγραμματισμού Python, καθώς συσκευάζουν αποσπάσματα κώδικα σε επαναχρησιμοποιήσιμα, αποτελεσματικά τμήματα κώδικα που έχουν σχεδιαστεί για να εκτελούν μια συγκεκριμένη εργασία. Οι συναρτήσεις μπορούν να κληθούν πολλές φορές μέσα σε ένα πρόγραμμα, διευκολύνοντας την οργάνωση του κώδικα και συμβάλλοντας στην αποφυγή επαναλαμβανόμενου κώδικα. Οι συναρτήσεις διευκολύνουν επίσης την αποσφαλμάτωση του κώδικα.
Important: Defining a function alone doesn’t make it run. Use start_thread to start a function in a new thread so it begins running right away at the start of the project, instead of waiting for an event or callback.
defdefines a function.returnsends the function’s output back to the main program.
Χρήση:
def function_name(parameters):
# Code to execute when the function is called
return result # Optional, used to return a value
Παράμετροι |
Περιγραφή |
|---|---|
|
Ένα όνομα που δίνετε στη συνάρτησή σας. |
|
Προαιρετικά. Μεταβλητές που δέχονται τιμές εισόδου όταν καλείται η συνάρτηση, επιτρέποντας τη διαβίβαση δεδομένων στη συνάρτηση. |
|
Optional. Lets the function send a result back to the caller. If a function does not include a return statement, it will return |
Σημείωση: Μια συνάρτηση πρέπει πάντα να ορίζεται πριν την κλήση της.
Ορισμός και κλήση συναρτήσεων#
Functions with No Parameters#
Εάν μια συνάρτηση δεν απαιτεί εισαγωγή δεδομένων, μπορείτε να την ορίσετε χωρίς παραμέτρους.
def greeting():
console.print("Hello!")
# Call the greeting function
greeting()
Functions with Parameters#
Μπορείτε επίσης να προσθέσετε παραμέτρους σε συναρτήσεις, οι οποίες σας επιτρέπουν να μεταβιβάζετε πληροφορίες που χρειάζεται η συνάρτηση για να λειτουργήσει.
def move_square(moves):
for index in range(moves):
drivetrain.drive_for(FORWARD, 150)
drivetrain.turn_for(RIGHT, 90)
# Call the move_square function
move_square(4)
Functions with Default Arguments#
Ένα προεπιλεγμένο όρισμα είναι μια παράμετρος που υποθέτει μια προεπιλεγμένη τιμή εάν δεν παρέχεται τιμή στην κλήση συνάρτησης για αυτό το όρισμα.
def move_square(moves=4):
for index in range(moves):
drivetrain.drive_for(FORWARD, 150)
drivetrain.turn_for(RIGHT, 90)
# Call the move_square function
move_square()
Return Values from Functions#
Functions can send data back to the caller using the return keyword. This allows you to capture and use the output in your program.
def times_two(number):
return number * 2
# Display the return value
console.print(times_two(2))
έναρξη_νήμα#
start_thread starts a function in a new thread so it begins running right away at the start of the project.
If more than one start_thread is used, the functions will run concurrently.
Usage:
start_thread(function)
Παράμετρος |
Περιγραφή |
|---|---|
|
Το όνομα μιας προηγουμένως ορισμένης συνάρτησης. |
def move_square():
# Move in a square path
while True:
drivetrain.drive_for(FORWARD, 150)
drivetrain.turn_for(RIGHT, 90)
def lights():
# Blink Optical Sensor lights
while True:
optical_sensor.set_light(LEDStateType.ON)
wait(0.5, SECONDS)
optical_sensor.set_light(LEDStateType.OFF)
wait(0.5, SECONDS)
# Start threads — Do not delete
start_thread(move_square)
start_thread(lights)