Show contents
Chapter 1 of 7 · 20 min
Writing your own functions
By the end of this chapter, you'll be able to
- Define a function with a return type and typed parameters
- Declare a function with a prototype before using it
- Write a void function that returns nothing
- Explain why a function can't change the variables passed to it
Defining and calling a function
You already know one function you wrote yourself: main. The others are written the same way:
#include <stdio.h>
double price_with_tax(double net_price, double rate) {
return net_price * (1 + rate);
}
int main(void) {
printf("%.2f\n", price_with_tax(100, 0.2));
printf("%.2f\n", price_with_tax(40, 0.18));
return 0;
}
120.00
47.20
doublebefore the name is the return type: the function returns adouble.(double net_price, double rate)are the parameters, each with its type.returnsends the value back to where the call was made and ends the function.- When calling,
price_with_tax(100, 0.2), the arguments 100 and 0.2 are copied into the parameters, in order.
A function that returns nothing has the type void. It doesn't need return, or it can use return; to stop early:
#include <stdio.h>
void line(int length) {
for (int i = 0; i < length; i++) {
printf("-");
}
printf("\n");
}
int main(void) {
line(10);
printf("Invoice\n");
line(10);
return 0;
}
----------
Invoice
----------
The prototype: announce before you use
The compiler reads your file from top to bottom. If main calls a function defined further down, it doesn't know it yet, and GCC 14 refuses to compile (implicit declaration of function). The solution: announce the function at the top of the file with its prototype, that is, its first line followed by a semicolon.
#include <stdio.h>
int square(int x); // prototype: name, parameters, return type
int main(void) {
printf("%d\n", square(9));
return 0;
}
int square(int x) { // definition: the code of the function
return x * x;
}
81
That's actually what #include <stdio.h> does: it inserts the prototypes of printf, scanf and the others into your file.
Pass-by-value
In C, arguments are copied into the parameters. The function works on its copy, never on the original variable.
Back in main: n has disappeared, and score is still 5.
Text description of the animation
- The function increase tries to add 10 to the variable it's given.
- In main, score is 5.
- We call increase. It's the value 5 that is passed, not the variable.
- The function has its own memory: its parameter n receives a copy, 5.
- n becomes 15. Only the copy changes.
- Back in main: n has disappeared, and score is still 5.
Your turn
Worked exercise
Worked exercise: a power function
Write a function long long power(int base, int exponent) that computes base to the power exponent (exponent zero or positive) with a loop, without using any library. Then display 2^10 = 1024 and 3^0 = 1.
Hint 1
base, exponent times.Hint 2
long long is a bigger integer than int. It's displayed with the format %lld.Show the solution and common mistakes
#include <stdio.h>
long long power(int base, int exponent) {
long long result = 1;
for (int i = 0; i < exponent; i++) {
result *= base;
}
return result;
}
int main(void) {
printf("2^10 = %lld\n", power(2, 10));
printf("3^0 = %lld\n", power(3, 0));
return 0;
}
2^10 = 1024
3^0 = 1
Common mistakes:
- Initialising
resultto 0: every multiplication gives 0. - Displaying a
long longwith%d:-Wallpoints it out, and the value displayed may be wrong. - Writing
2^10in the code: in C,^isn't power but a bitwise "exclusive or", and2 ^ 10is 8.
Summary
- 1A function has a return type, a name and typed parameters; void means "returns nothing".
- 2The compiler reads from top to bottom: declare a prototype before calling a function defined further down.
- 3Arguments are copied into the parameters: this is pass-by-value.
- 4A function can't change the caller's variables, unless it's given their address.
- 5Prefer returning a result with return over displaying inside the function.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.