Summary
In this lesson, you learned that:
- A function is a named piece of code that you write once and can run as many times as you need. When you change it, the change applies to every call.
- A function definition has a return type, a name, parameters in parentheses and a body in curly braces:
int add(int a, int b) { ... }.voidas the return type means that the function returns nothing, and(void)in the parentheses means that it takes nothing. A definition alone doesn't run anything. - You call a function with its name, the arguments in parentheses and a semicolon:
print_line(5);. The program starts inmain, and other functions are called from there.printf,strlenandtoupperare functions too. - The compiler must know a function before it is called: define the function above
main, or write its prototype abovemain, such asint add(int a, int b);. Otherwise, you get the errorcall to undeclared function. Header files such asstdio.hhold the prototypes of library functions. - A parameter is a variable in the definition, an argument is a value in the call. The arguments go into the parameters in order, and their number and types must fit.
returngives a value back to the place of the call, so you can use the call like a value:int total = add(2, 3) * 2;. It also ends the function right away. A function that isn'tvoidmust return a value in every case, otherwise the program doesn't compile here. In avoidfunction,return;ends it early.printfonly shows a value; the program can't work with it any further. A function that calculates something should return the result and leave printing to the code that called it.- The variables of a function and its parameters are local: they exist only in that function, and that applies to
maintoo. A parameter gets a copy of the argument, so changing it doesn't change the caller's variable. Values go into a function through its parameters and come out withreturn. - An array parameter is written
int numbers[], and the function also needs the number of elements as another parameter. The function works on the caller's array itself, so its changes stay. A string that the function only reads is aconst char text[], and the function finds its end by the'\0'. - The library has ready-made functions for math in
math.h:sqrt,pow,roundandfabs, which return adouble.absfor whole numbers is instdlib.h.
In the next lesson, on pointers, you will find out what addresses in memory are, how a function can change a variable of the code that called it, and why arrays are not copied.
Instructions
Let's practice. Above main, write a function is_even with a parameter number of type int that returns a bool: true when the number is even, otherwise false (add #include <stdbool.h>). Below it, write a function count_even with two parameters, an array numbers and its count, that returns how many elements of the array are even. Use is_even in it. At the end of main, before return 0;, print the result of count_even(numbers, COUNT) with a label: Even numbers: 4.
Write a function average with the same two parameters that returns the average of the elements as a double. Below the previous line, print it with two decimal places: Average: 5.67.
Start programming for free
12/12