Functions
Functions: parameters, return values and decomposing a solution.
A function is a named piece of code you can call as many times as you like. Functions make solutions readable: the main logic stays in main while the details move into separate functions.
A function is declared like this: result type, name, parameters in parentheses. The return keyword returns the result and ends the function.
C++
A greatest-common-divisor function (Euclid's algorithm)
This is Euclid's algorithm — computing the greatest common divisor (GCD). Note that the function is declared above main; otherwise the compiler would not "see" it.
Functions can return bool — a convenient way to express checks. The function below determines whether a number is prime: it is enough to test divisors only up to the square root of n, so the loop runs while i * i <= n. This is a genuine algorithmic optimization: a million checks instead of a trillion.
C++
Primality check up to the square root. Enter, for example: 97
Task: write a function int digitSum(long long n) that returns the sum of a number's digits (hint: peel digits off with n % 10 and n /= 10).
Then solve the attached problems "GCD of Two Numbers" and "Prime Check" — and mark the lesson as completed.