Functions · פונקציות
Name a block of code
- A function is named code you can use again and again.
- Make one with
def name():and an indented body. - "Calling" the function by its name runs that code.
הגדר בלוק קוד
- פונקציה היא קוד שמקבל שם וניתן להשתמש בו שוב ושוב.
- צור אחד עם
def name():וגוף מוזח. - "קריאה" לפונקציה בשמה מפעילה את הקוד הזה.
Give it inputs (parameters)
- The names in the brackets are parameters — the function's inputs.
- When you call it, you pass in arguments (the real values).
- A function can take more than one parameter.
ספק לה כניסות (פרמטרים)
- השמות בתוך הסוגריים הם פרמטרים — הכניסות של הפונקציה.
- כאשר אתה קורא לה, אתה מעביר ארגומנטים (הערכים האמיתיים).
- פונקציה יכולה לקבל יותר מפרימטר אחד.
def shout(word):
print(word.upper() + "!")
shout("hello")
Send back a result (return)
returnsends a value back to whoever called the function.- You can store that value or use it in an expression.
returnalso ends the function straight away.
שלח חזרה תוצאה (return)
returnשולח ערך חזרה למי שהתקשר לפונקציה.- ניתן לאחסן את הערך הזה או להשתמש בו בהבעה.
returnגם מסיים את הפונקציה מיידית.
def double(x):
return x * 2
result = double(5)
print(result)
return vs print
printonly shows something on the screen.returngives a value back that the rest of your code can use.- A function with no
returngives backNone.
return לעומת print
printמציג רק משהו על המסך.returnמחזיר ערך ששאר הקוד שלך יכול להשתמש בו.- פונקציה ללא
returnמחזירהNone.
In Cambridge pseudocode
- A
FUNCTIONreturns a value; aPROCEDUREjust does an action.
בפסאודוקוד קמברידג'
- ה-
FUNCTIONמחזיר ערך; ה-PROCEDUREפשוט מבצע פעולה.
FUNCTION Add(A : INTEGER, B : INTEGER) RETURNS INTEGER
RETURN A + B
ENDFUNCTION
PROCEDURE Greet(Name : STRING)
OUTPUT "Hello, ", Name
ENDPROCEDURE
Common mistakes
- A function returns
Noneunless you writereturn; printing is not the same as returning. - Run a function with
():greet(), notgreet. - Define a function before you call it.
טעויות נפוצות
- פונקציה מחזירה
Noneאלא אם כן אתה כותבreturn; הדפסה אינה זהה להחזרת ערך. - הרץ פונקציה עם
():greet(), לאgreet. - הגדר פונקציה לפני שאתה קורא אותה.
Now you try
- Define each function, then the checks will call it for you.
- Use
returnto send the answer back (do notprintit).
כעת תנסו בעצמכם
- הגדר כל פונקציה, ואז הבדיקות יקראו אותה עבורך.
- השתמש ב-
returnכדי לשלוח את התשובה חזרה (אל תעשה לה этоprint).
Calling a function · קריאה לפונקציה
A call pushes a frame with its arguments; return pops it. · קריאה דוחפת פריס עם הפרמטרים שלה; return מוציא אותה החוצה.
Write a function add(a, b) that returns the sum of a and b. · כתוב פונקציה add(a, b) שתחזיר את הסכום של a ו-b.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write a function greet(name) that returns the string Hello, then the name then !. So greet("Sam") returns Hello, Sam!. · כתוב פונקציה greet(name) שתחזיר את המחרוזת Hello, , אחר כך את השם, ואז את !. לכן greet("Sam") מחזירה Hello, Sam!.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write a function biggest(a, b) that returns the larger of the two values. If they are equal, return either one. · כתוב פונקציה biggest(a, b) שתחזיר את הערך הגדול יותר משני הערכים. אם הם שווים, החזר אחד מהם.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write is_palindrome(word) that returns True when the word reads the same backwards (like racecar), otherwise False. · כתוב is_palindrome(word) שתחזיר True כאשר המילה נקראת אותו דבר גם בהפוך (כמו racecar), אחרת False.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.