DMarlon Studio

Lesson 13 · 13 of 20

Parameters, returns and prototypes · C

Declare prototypes in C so functions can come after main, pass several parameters by value and use several return statements in one function.

C · Parameters, returns and prototypes in DMarlon Studio: the example's code and its output in the terminal

A prototype is a function's first line ending in a semicolon: double apply_discount(double price, int percent);. It tells the compiler the name, the parameters and the return type, so main can use the function even though its body comes further down. In long programs, main stays at the top and is read first.

When you call the function, the arguments are handed out in the same order as the parameters: in apply_discount(59.90, 20), price is 59.90 and percent is 20.

In C parameters are passed by value: the function gets a copy. Even though apply_discount changes its own price, the price variable in main is still 10.00. If you call it without storing the result, the result is lost; to keep it, assign it: price = apply_discount(price, 50);.

A function can have several `return` statements: the first one reached ends it. is_leap applies the calendar rules in order: a multiple of 400 is a leap year; of 100, it is not; otherwise it is a leap year if it is a multiple of 4. That is why 1900 is not and 2000 is.

Code · main.c

#include <stdio.h>// Prototypes announce the functions; their bodies come after main.double apply_discount(double price, int percent);int is_leap(int year);int main(void) {    // Arguments go in the same order as the parameters.    printf("Headphones: %.2f\n", apply_discount(59.90, 20));    printf("Keyboard:   %.2f\n", apply_discount(45.00, 15));    // Parameters are copies: the function cannot change price here.    double price = 10.0;    apply_discount(price, 50);    printf("price is still %.2f\n", price);    price = apply_discount(price, 50);  // keep the returned value    printf("now price is %.2f\n", price);    int years[] = {1900, 2000, 2024, 2026};    for (int i = 0; i < 4; i++)        printf("%d: %s\n", years[i], is_leap(years[i]) ? "leap" : "normal");    return 0;}double apply_discount(double price, int percent) {    price = price * (100 - percent) / 100;  // changes only the copy    return price;}int is_leap(int year) {    if (year % 400 == 0) return 1;  // return ends the function at once    if (year % 100 == 0) return 0;    return year % 4 == 0;}

Output · cc main.c -o main && ./main

Headphones: 47.92
Keyboard:   38.25
price is still 10.00
now price is 5.00
1900: normal
2000: leap
2024: leap
2026: normal

Key points

  • A prototype announces a function before it is defined.
  • Arguments are matched to parameters by order.
  • Parameters are copies: the function does not change the caller's variables.
  • Store the returned value if you need it.
  • The first return reached ends the function.

Exercise

Add the prototype and the function int days_in_february(int year), which uses is_leap to return 28 or 29, and print the result for the four years.