All lessons

Functions

9 min · C

Package code under a name so you can reuse it.

C · Functions0/5 cleared

Enemy 1 of 5

Declaring and calling a function

A function packages a block of code under a name so you can call it again from anywhere instead of retyping the same logic every time you need it. This keeps programs shorter and, importantly, means if the logic needs to change, you only fix it in one place.

In C, every function declares the type of value it returns, plus the type of each parameter it takes. int doubleIt(int n) means "a function called doubleIt, taking one int parameter n, returning an int" — the compiler uses these declared types to check that every call site passes the right kind of value and uses the result correctly.

return hands a value back to whoever called the function, and immediately ends the function's execution — any code after a return in the same block never runs. Functions need to be defined (or at least declared) above the point in the file where they're first called, which is why doubleIt is written above main here.

You
Syntax Skeleton

Loading editor...

Ln 1, Col 1Spaces: 4
Sign up to fightTarget output: 10