Functions

Functions: parameters, multiple return values, GCD and primality.

A function in Go is declared with the word func; the result type is written after the parameter list. A language highlight — a function can return several values at once. Below is Euclid's algorithm for the greatest common divisor.
Go

GCD of two numbers. Enter: 12 8

The line a, b = b, a%b is a multiple assignment: both sides are evaluated simultaneously, no temporary variable needed. Functions returning bool express checks. For primality, divisors are tried up to the square root: the condition i*i <= n.
Go

Primality check up to the square root. Enter: 97

An example of multiple return values: func minMax(a []int) (int, int) can return the minimum and maximum in a single pass — try writing that function as your task. Then solve the attached problems "GCD of Two Numbers" and "Prime Check" — and mark the lesson as completed.

Practice problems

Доска