Functions
In this lesson: you will give a name to a part of a program and use it several times.
Lesson 13
In this lesson: you will give a name to a part of a program and use it several times.
In this lesson: you will give a name to a part of a program and use it several times.
A function (функсия) is a part of a program with a name. You write it once and call it as many times as you like:
1функсия салом() {2чоп("Салом!");3чоп("Хуш омадед!");4}56салом();7салом();
функсия салом() { … } declares a function. Nothing is printed yet.салом(); calls the function: the commands inside it run.You can give values to a function. They are called parameters:
1функсия саломДиҳ(ном: сатр) {2чоп("Салом, " + ном + "!");3}45саломДиҳ("Зарина");6саломДиҳ("Фарҳод");
ном: сатр is the parameter ном of type сатр (string). The type says what value is expected: сатр (string), рақам (number) or мантиқӣ (logical). If you pass a number instead of a string, the playground warns you.
1функсия хат(аломат: сатр, шумора: рақам) {2тағ натиҷа = "";3барои (тағ и = 0; и < шумора; и++) {4натиҷа += аломат;5}6чоп(натиҷа);7}89хат("*", 5);10хат("-", 10);
салом; instead of салом();.Write a function саломДиҳ(ном) ("greet(name)") that prints Салом, ном! ("Hello, name!"). Read two names and call the function for each of them.
Input: Two lines: two names.
Output: Two greetings.
Write a function хат(н) ("line") that prints н equals signs = on one line. Read a number and call the function.
Input: One number.
Output: One line of =.
Write a function ҷадвал(н) ("table") that prints the lines from н × 1 = … to н × 5 = …. Read a number and call the function.
Input: One number.
Output: Five lines.