Lesson 14 of 23

Functions

10 min 25 XP

A function is a named block of code with a return type, a name and parameters. If you define it below main(), declare a prototype above main().

Example: edit me and press Run

Output Loading C (first time takes a few seconds)
Press Run to see the result here.

C passes arguments by value: changing a parameter inside a function doesn't change the caller's variable. To change it you need pointers (next module).

Example: edit me and press Run

Output Loading C (first time takes a few seconds)
Press Run to see the result here.

Challenge

+25 XP

Write a function int gcd(int a, int b) using Euclid's method. In main(), read a and b and print the GCD and LCM. Input: 12 18 Output: GCD: 6 LCM: 36

Output Loading C (first time takes a few seconds)
Press Run to see the result here.