Functions, parameters, and return values · פונקציות, פרמטרים וערכי החזרה
Why functions
- A function is a named piece of work. You write it once and call it whenever you need it.
- Functions keep programs short and clear, and let you build big jobs from small ones.
- You have already used
printf— a function from<stdio.h>. Now you write your own.
למה פונקציות
- פונקציה היא חלק קרוי מהעבודה. אתה כותב אותה פעם אחת וקורא לה בכל פעם שיש צורך בה.
- פונקציות שומרות על הקוד קצר ומובן, ומאפשרות לבנות משימות גדולות ממשימות קטנות.
- כבר השתמשת ב-
printf— פונקציה מ-<stdio.h>. עכשיו תכתוב את משלך.
Parameters and return values
- The values you pass in are the parameters. The value you hand back is the return value.
int max2(int a, int b)takes twointparameters and returns oneint.return x;ends the function right away and givesxback to the caller.
פרמטרים וערכי החזרה
- הערכים שמעבירים לתוכן הם ה-פרמטרים. הערך שמחזירים החוצה הוא ערך ההחזרה.
int max2(int a, int b)מקבל שניintומחזירintאחד.return x;מסתיים מיידית ומוחזרxלקורא.
void functions, and building big from small
- A function that returns nothing has the return type
void. It does work but hands back no value. - One function can call another.
max3can callmax2twice to do its job. - Small functions that each do one thing are easier to read and to fix.
פונקציות void, ובנייה גדולה מטווח קטן
- לפונקציה שאינה מחזירה ערך יש סוג החזרה
void. היא עובדת אך אינה מחזירה ערך כלשהו. - פונקציה אחת יכולה לקרוא לפונקציה אחרת.
max3יכול לקרוא ל-max2פעמיים כדי לבצע את משימתו. - פונקציות קטנות שמבצעות פעולה אחת הן קלות יותר לקריאה ולתיקון.
C copies your arguments
- When you call a function, C gives it a copy of each argument.
- So changing a parameter inside the function does not change the caller's variable.
- Keep this in mind — in a later lesson, pointers let a function reach the original.
C מעתיקה את הפרמטרים שלך
- כאשר קוראים לפונקציה, C מעבירה לה עתיקה של כל פרמטר.
- לכן שינוי פרמטר בתוך הפונקציה אינו משנה את המשתנה של הקורא.
- זכרו זאת — בשיעור הבא, מצביעים מאפשרים לפונקציה לגשת למקור.
#include <stdio.h>
void try_double(int x) {
x = x * 2; // changes only the COPY
}
int main(void) {
int n = 5;
try_double(n);
printf("%d\n", n); // still 5 — the original did not change
return 0;
}
Common mistakes
- A function used before it is defined needs a prototype above
main. - Arguments pass by value — pass a pointer to change the caller's variable.
טעויות נפוצות
- פונקציה המשמשת לפני ההגדרה שלה צריכה הגדרה (prototyping) מעל
main. - פרמטרים עוברים בערך — העברו מצביע כדי לשנות את המשתנה של הקורא.
Now you try
- Write each function with the exact name and parameters shown, and
returnthe right value. - You may call one of your functions from another.
- Do not write a
main— the checker provides one.
כעת תנסו בעצמכם
- כתבו כל פונקציה עם השם והפרמטרים המדויקים כפי שמוצג, ו-
returnאת הערך הנכון. - תוכלו לקרוא לאחת מפונקציותיכם מתוך פונקציה אחרת.
- אל תכתוב
main— הבוקר מספק אותו already.
Functions: call and return · פונקציות: קריאה והחזרה
A call pushes a frame with its parameters; return pops it. · קריאה דוחפת פריט עם הפרמטרים שלה; return מוצאת אותו.
Write int max2(int a, int b) returning the larger of two numbers, and int max3(int a, int b, int c) returning the largest of three. Make max3 call max2. Do not write a main. · כתוב int max2(int a, int b) שמחזיר את הגדול ביותר מבין שני מספרים, ו-int max3(int a, int b, int c) שמחזיר את הגדול ביותר מבין שלושה. גרוֹר ל-max3 לקרוא ל-max2. אל תכתב main.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Complete int power(int base, int exp) so it returns base raised to exp (assume exp >= 0). Use a loop. power(2, 10) is 1024. Do not write a main. · שלים את int power(int base, int exp) כך שתחזיר ⟨⟩base מעוצה ל-exp (הנח that exp >= 0). השתמש בלולאה. power(2, 10) הוא 1024. אל תכתוב main.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Complete int clamp(int x, int lo, int hi) so it keeps x within [lo, hi]: return lo if x is below lo, hi if x is above hi, otherwise x. Do not write a main. · השלם int clamp(int x, int lo, int hi) כך שישמור על x בתוך [lo, hi]: החזר lo אם x נמוך מ-lo, hi אם x גבוה מ-hi, אחרת x. אל תכתב main.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.