KevsRobots Learning Platform
50% Percent Complete
By Kevin McAleer, 3 Minutes
Page last updated June 15, 2025
As your programs grow, it becomes helpful to organize your code into functions.
Functions let you name a block of code and call it whenever you need it — this keeps your code clean, reusable, and easier to understand.
A function is like a mini-program you can define and reuse.
Every C program starts with a special function called main()
— that’s where the program begins.
Here’s a simple function:
void greet() {
printf("Hello from a function!\n");
}
This function uses the printf()
function to print a message. Functions can contain other functions, variables, and logic just like the main program.
The \n
at the end of the string is a newline character, which moves the cursor to the next line after printing.
To run the function, just call it by name:
int main() {
greet(); // this runs the greet() function
return 0;
}
Syntax
means the rules for how to write code correctly.
Here’s the basic structure of a function in C:
return_type function_name(parameter_list) {
// code to execute
}
The parts are:
int
, void
, etc.)greet
)int a, int b
)void say_hello(char name[]) {
printf("Hello, %s!\n", name);
}
the char name[]
part is a parameter that lets you pass in a name when you call the function. The square brackets []
indicate that name
is an list of characters (a string).
Call it like this:
say_hello("Kevin");
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(2, 3);
printf("Result: %d\n", result);
return 0;
}
Quick Tips
- Declare functions before calling them (or use a function prototype)
- Always match the number and type of arguments
void
means “no return value”
You now know how to:
Next up: Setting Up the Toolchain, where we’ll install the software you need to compile and upload C programs to the Raspberry Pi Pico.
You can use the arrows ← →
on your keyboard to navigate between lessons.