Lesson 14

A function with бозгашт

A function with бозгашт

In this lesson: a function calculates a result and returns it.

The whole lesson on one page

A function with бозгашт

In this lesson: a function calculates a result and returns it.

бозгашт

бозгашт means "return":

1функсия квадрат(х: рақам): рақам {
2 бозгашт х * х;
3}
4
5чоп(квадрат(4));
6тағ натиҷа = квадрат(3) + квадрат(4);
7чоп(натиҷа);
  • бозгашт х * х; returns the value to the place where the function was called.
  • : рақам after the brackets says that the function returns a number.
  • After бозгашт, the function ends.

A function with a condition

1функсия калонтарин(а: рақам, б: рақам): рақам {
2 агар (а > б) {
3 бозгашт а;
4 }
5 бозгашт б;
6}
7
8чоп(калонтарин(3, 7));
9чоп(калонтарин(калонтарин(1, 9), 4));

A function that returns дуруст or нодуруст

1функсия ҷуфтАст(н: рақам): мантиқӣ {
2 бозгашт н % 2 === 0;
3}
4
5тағ рақам = хонданиРақам();
6агар (ҷуфтАст(рақам)) {
7 чоп("Ҷуфт");
8} вагарна {
9 чоп("Тоқ");
10}
Input
12

чоп and бозгашт

чоп shows a value on the screen; бозгашт gives it back to the program so that you can use it: in a calculation, in a variable or in a condition.

Task: Temperature

Write a function фаренгейт(с) ("fahrenheit") that converts a temperature from degrees Celsius to degrees Fahrenheit and returns it: с * 9 / 5 + 32. Read a temperature and print the result.

Input: One number: degrees Celsius.

Output: Degrees Fahrenheit.

Task: Prime number

Write a function соддаАст(н) ("isPrime(n)") that returns дуруст ("true") if н is a prime number (greater than 1 and divisible only by 1 and by itself). Read a number and print Содда ("Prime") or Содда нест ("Not prime").

Input: One whole number.

Output: Содда or Содда нест

Task: Power

Write a function дараҷа(асос, нишондиҳанда) ("power(base, exponent)") with a loop (without Риёзӣ.қувват and **). Read two numbers and print the result.

Input: Two numbers: the base, then the exponent (0 or more).

Output: The power.

Task: The greatest of three

Write a function калонтарин(а, б) ("greatest(a, b)") that returns the greater of two numbers. Read three numbers and print the greatest of them with the help of this function.

Input: Three numbers.

Output: The greatest.