Move repeated calculations into functions; learn to write prototypes, why arguments are passed as copies, and how to split code into .h/.c files.
Imagine using the voltage divider formula in five different places in a project. When you find a mistake in the formula, you have to fix five places separately; forget one and the program gives inconsistent results. Functions solve this: you write the calculation once, give it a name and call it from wherever you like. Microcontroller libraries work exactly like this: they are collections of small, well-named functions such as adc_oku() or led_yak().
Our first functions
#include <stdio.h>
double guc_hesapla(double v, double i) {
return v * i;
}
double adc_volt(int adc, double vref) {
return adc * vref / 1023.0;
}
int main(void) {
printf("Guc: %.1f W\n", guc_hesapla(230.0, 4.2));
printf("ADC 818 -> %.2f V\n", adc_volt(818, 5.0));
printf("ADC 818 -> %.2f V (3.3 V ref)\n", adc_volt(818, 3.3));
return 0;
}
Guc: 966.0 W
ADC 818 -> 4.00 V
ADC 818 -> 2.64 V (3.3 V ref)
The first line of a function tells you four things:
- Return type (
double): the type of the value the function gives back. - Name (
guc_hesapla): a name that says what it does. - Parameters (
double v, double i): the values it takes from outside and their types. - Body: sends the result back to the caller with
return.
We called the same adc_volt function for a 5 V and a 3.3 V reference. The formula lives in one place and is used in two.
Prototypes: declare first, define later
The C compiler reads a file from top to bottom. If you call a function before its definition, the compiler doesn't know it. The solution is to put a prototype (function declaration) at the top of the file: just the first line, followed by a semicolon.
#include <stdio.h>
double gerilim_bolucu(double vin, double r1, double r2); // prototip
int main(void) {
double vout = gerilim_bolucu(5.0, 10000.0, 10000.0);
printf("Vout = %.2f V\n", vout);
printf("Vout = %.2f V\n", gerilim_bolucu(12.0, 22000.0, 4700.0));
return 0;
}
double gerilim_bolucu(double vin, double r1, double r2) {
return vin * r2 / (r1 + r2);
}
Vout = 2.50 V
Vout = 2.11 V
Thanks to the prototype, the compiler also checks that each call passes the right number and types of arguments. And main stays at the top, so anyone reading from the top sees right away what the program does.
void functions and pass by value
Functions that return nothing have the return type void. Now let's look at a very important rule of C: arguments are passed to a function as copies.
#include <stdio.h>
void rapor_yaz(int kanal, double deger) {
printf("Kanal %d: %.2f V\n", kanal, deger);
}
void sifirla(int x) {
x = 0;
printf("Fonksiyon icinde x = %d\n", x);
}
int main(void) {
int sayac = 42;
rapor_yaz(1, 3.3);
sifirla(sayac);
printf("main icinde sayac = %d\n", sayac);
return 0;
}
Kanal 1: 3.30 V
Fonksiyon icinde x = 0
main icinde sayac = 42
The sifirla function didn't receive sayac itself, only a copy of the value 42. Zeroing the copy doesn't affect the original variable. This makes functions safe, but sometimes we really do want a function to change a variable outside it. The way to do that is pointers; we'll fix this example in lesson 9. Also, variables defined inside a function live only in that function: x is not visible from main.
The idea of header files: splitting code into files
As a program grows, keeping every function in one file gets hard. The common layout in C is this: prototypes go into a header file (.h), definitions go into a source file (.c).
/* elektrik.h */
#ifndef ELEKTRIK_H
#define ELEKTRIK_H
double guc_hesapla(double v, double i);
double gerilim_bolucu(double vin, double r1, double r2);
#endif
/* elektrik.c */
#include "elektrik.h"
double guc_hesapla(double v, double i) {
return v * i;
}
double gerilim_bolucu(double vin, double r1, double r2) {
return vin * r2 / (r1 + r2);
}
/* main.c */
#include <stdio.h>
#include "elektrik.h"
int main(void) {
printf("P = %.1f W\n", guc_hesapla(12.0, 1.5));
printf("Vout = %.2f V\n", gerilim_bolucu(3.3, 1000.0, 2000.0));
return 0;
}
gcc -Wall -Wextra -std=c11 main.c elektrik.c -o hesap
./hesap
P = 18.0 W
Vout = 2.20 V
#include "elektrik.h": Quotes mean "look in my own folder first". Angle brackets (<stdio.h>) are for system headers.#ifndef / #define / #endif: an include guard. If the header is included twice in the same compilation, it stops the contents from being processed a second time.- When compiling, we pass both
.cfiles together; the linker matches the calls inmain.cwith the definitions inelektrik.c.
Arduino and PIC libraries come in this layout too: you include only the .h file and the build system takes care of the rest.
Common mistakes
Calling without a prototype: for a function called before its definition and without a prototype, older GCC versions give an "implicit declaration" warning and newer versions give an error. Don't ignore the warning; a return value of the wrong type can be silently corrupted.
- Expecting a function to change its argument: with pass by value, the function only changes the copy.
- Forgetting return in a function whose return type is double: the caller gets a garbage value.
-Wallcatches this with the warning "control reaches end of non-void function".
Exercise
Write a function that calculates the series resistor for an LED: double led_direnci(double vkaynak, double vled, double i_ma). The formula is R = (Vsupply − Vled) / I; since the current is given in milliamps, don't forget to convert it to amps. Print the result for a 2.0 V red LED at 5 V and a 3.2 V white LED at 12 V, both at 20 mA. Use a prototype.
Hint: i_ma / 1000.0 gives the value in amps. Print the result in whole ohms with %.0f.
Solution
#include <stdio.h>
double led_direnci(double vkaynak, double vled, double i_ma);
int main(void) {
printf("Kirmizi LED: %.0f ohm\n", led_direnci(5.0, 2.0, 20.0));
printf("Beyaz LED : %.0f ohm\n", led_direnci(12.0, 3.2, 20.0));
return 0;
}
double led_direnci(double vkaynak, double vled, double i_ma) {
return (vkaynak - vled) / (i_ma / 1000.0);
}
Kirmizi LED: 150 ohm
Beyaz LED : 440 ohm
You've moved calculations into functions and learned to split code into files. In the next lesson we'll keep many measurements in arrays and see how text is really stored in C.
// Spot the bug +10 XP
Exactly one line below is wrong. Click the faulty line.
Line 3 declares the return type as void, but the function returns a value and main uses it. A void function cannot return a value; the compiler reports an error. Correct version: double guc_hesapla(double v, double i)
// Mini quiz
Click an option; you will see right away, with the reason, whether it is correct or not.
Mark it when you finish: your progress is saved and you earn +20 XP.
Tip: ← → keys let you move between lessons.


