cint

cint university / 101 / ced, the cint editor

101-10: functions

This is lesson 101-10 of CU-101.2, checked against cint 0.2.0.

You will give a calculation a name, use it as often as you like with different numbers and start a program from a function called main.

the program

I64 area(I64 width, I64 height) {
    return width * height;
}

I64 larger(I64 a, I64 b) {
    if (a > b) { return a; }
    return b;
}

void main() {
    I64 kitchen = area(4, 3);
    I64 bedroom = area(5, 4);
    "kitchen {kitchen}, bedroom {bedroom}, larger {larger(kitchen, bedroom)}\n";
}

Run this program in ced

Run it:

kitchen 12, bedroom 20, larger 20

what happened

A function is a calculation with a name. The first three lines make a function called area. Read its first line from left to right:

  • I64 is the type of the answer it gives back.
  • area is its name.
  • (I64 width, I64 height) are its parameters: the numbers it needs, each with a type and a name.

The lines in the braces are its body. return width * height; works out the answer and gives it back.

Calling a function runs it. area(4, 3) runs area with width set to 4 and height set to 3, and the call is replaced by the answer, 12. The numbers in a call are its arguments, and they fill the parameters in order. area(5, 4) runs the same lines again with different numbers. A function is written once and used as often as you need.

A function can make choices. larger returns a if it is bigger and b otherwise. A return ends the function at once, so when a > b, the last line never runs.

main is where the program starts. Until now, every program ran from its first line to its last. A program can instead start from a function called main, and cint runs main when the program starts. void in front of main means it gives back no answer. From lesson 101-12 on, programs start from main, because in cint 0.2.0 a list of numbers can only be made inside a function.

Each function has its own names. width and height exist only inside area, and kitchen exists only inside main. Two functions can use the same name for different things without mixing them up.

change it

  1. Add a room hall that is 6 by 2, and print its area.
  2. Write a function perimeter that gives back the distance around a room: twice the width plus twice the height. Print the kitchen's perimeter.
  3. Use larger twice in one line to print the largest of three rooms.
one way to write perimeter
I64 perimeter(I64 width, I64 height) {
    return 2 * width + 2 * height;
}

Run this program in ced

`"{perimeter(4, 3)}\n";` prints 14. A program that has functions but no `main` runs its other lines from the top, so this line can sit below the function.

when it goes wrong

A call needs one argument for each parameter. area(3) leaves out the height, and cint says the arguments are wrong. The same error code is also used for fields, which 101 does not teach:

I64 area(I64 w, I64 h) { return w * h; }
void main() { "{area(3)}\n"; }

Run this program in ced

error[C2022]: wrong fields or arguments
  --> argcount.ci:2:17
   |
 2 | void main() { "{area(3)}\n"; }
   |                 ^
   |

A misspelled name is not a function cint knows. Calling are instead of area gives the message from lesson 101-03:

I64 area(I64 width, I64 height) {
    return width * height;
}
void main() {
    "{are(4, 3)}\n";
}

Run this program in ced

error[C3005]: undefined or unusable name
  --> unknownfn.ci:5:7
   |
 5 |     "{are(4, 3)}\n";
   |       ^
   |

new words

function, parameter, argument, return

Next: lesson 101-11, functions that call themselves.