Συναρτήσεις#
Εισαγωγή#
Οι συναρτήσεις αποτελούν θεμελιώδες στοιχείο του προγραμματισμού 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!")
def main():
# Call the greeting function
greeting()
# Start threads — Do not delete
start_thread(main)
Functions with Parameters#
Μπορείτε επίσης να προσθέσετε παραμέτρους σε συναρτήσεις, οι οποίες σας επιτρέπουν να μεταβιβάζετε πληροφορίες που χρειάζεται η συνάρτηση για να λειτουργήσει.
# Build Used: Super Code Base 2.0
def move_square(moves):
for index in range(moves):
drivetrain.drive_for(FORWARD, 150)
drivetrain.turn_for(RIGHT, 90)
def main():
# Call the move_square function
move_square(4)
# Start threads — Do not delete
start_thread(main)
Functions with Default Arguments#
Ένα προεπιλεγμένο όρισμα είναι μια παράμετρος που υποθέτει μια προεπιλεγμένη τιμή εάν δεν παρέχεται τιμή στην κλήση συνάρτησης για αυτό το όρισμα.
# Build Used: Super Code Base 2.0
def move_square(moves=4):
for index in range(moves):
drivetrain.drive_for(FORWARD, 150)
drivetrain.turn_for(RIGHT, 90)
def main():
# Call the move_square function
move_square()
# Start threads — Do not delete
start_thread(main)
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
def main():
# Display the return value
console.print(times_two(2))
# Start threads — Do not delete
start_thread(main)
έναρξη_νήμα#
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)
Παράμετροι |
Περιγραφή |
|---|---|
|
Το όνομα μιας προηγουμένως ορισμένης συνάρτησης. |
# Build Used: Super Code Base 2.0
def move_square():
# Move in a square path
while True:
drivetrain.drive_for(FORWARD, 150)
drivetrain.turn_for(RIGHT, 90)
def led_lights():
# Blink LED lights
while True:
bumper.set_color(GREEN)
wait(0.5, SECONDS)
bumper.set_color(RED)
wait(0.5, SECONDS)
# Start threads — Do not delete
start_thread(move_square)
start_thread(led_lights)