Add two numbers
Use the inputs a function receives. An easy embedded C problem (First steps in C), run and graded in the browser.
easy · First steps in C · functions · parameters
The names in the parentheses are the function's parameters. They hold the values the caller passed in.
Return the sum of a and b.
Starter code
int add(int a, int b) {
// a and b hold the two numbers to add.
return 0;
}Example tests
Two positive numbers
printf("%d\n", add(2, 3));Expected output:
5A negative number
printf("%d\n", add(-4, 10));Expected output:
6Learn it first
- Microcontrollers and your first program · What a microcontroller is, how code gets onto it, and the parts of a C program.
More First steps in C problems
- Return a value · Make a function hand back a number.
- Fix a compiler error · Read an error message and repair the code.
- Minutes to seconds · Convert a duration with one multiplication.
- Area of a rectangle · Combine two parameters into one result.
- Print a line · Send text to the output with printf.
- Print a number · Put a value into printed text with a placeholder.