Gilad Ri
Posted on June 13, 2019
Awesome, we get to talk about functions, and after that, we can finally start to write a real code!
In my notebook, I made a comparison between functions and factories. So, let's talk about factories. (Yes the real world things with machines and workers).
Let's imagine we have a coffee machines factory. That means that our lovely and successful factory buy cables, some metal, and plastic (and perhaps some more raw materials) and from all that it produces coffee machines. A lot of them.
Ok, so in other words, our factory gets some raw materials (cables, metal, plastic) as input and produces the coffee machine as output.
A coffee machine is a factory too. Its input is a coffee capsule, water, and milk (optional) and its output is coffee. And coffee is input for a programmer, who produces some code and a lot of bugs as output.
In conclusion, we understand that we surrounded by factories/functions on a daily basis. A function is just a block of code (so yeah, it's bounded by curly brackets) which can get variables as inputs (in the example above: raw materials) and returns output according to its type.
Simply put, the function format is:
[output_type] [function_name]([inputs]) {
// some code
}
The inputs format is:
([type1] [variable1], [type2] [variable2], ...)
The function of our coffee machines factory may be:
coffeeMachine makeCoffeeMachine(cable c, metal m, plastic p) {
coffeeMachine newMachine;
build(&newMachine, c, m, p);
return newMachine;
}
And you can see that our function calls another function: "build". This function builds the new coffee machine somehow (we don't really care how) and then we can return it as output.
Good, now let's talk about real functions. The first function we will talk about is "main". The main function it is the most important function in your code. If you don't have one (and only one) "the computer" won't let you to run your code. The readers who were curious enough probably already saw some main functions before. Let's write one by ourselves:
#include <stdio.h>
void main() {
int x = 10;
int y = 5;
if (x > y) {
printf("The max number is %d\n", x);
} else {
// y is equal or higher than x
printf("The max number is %d\n", y);
}
}
As you can see, our function initializes two variables and then prints the sum of them. Let's build some more function, which helps the main function to be more compact:
#include <stdio.h>
/*
* The function gets 2 integers and returns the max value
*/
int max(int a, int b) {
if (a > b) {
return a;
} else {
// b is bigger or equal
return b;
}
}
/*
* The main function
*/
void main() {
int x = 10;
int y = 5;
printf("The max number is %d\n", max(x,y));
}
As you can see, our new function max gets two integers as input and returns the max value as output. Notice that we can name the variables of each function in any way we want. The only thing that's important it's the order of the variables. In the example above, the "box" we named 'x' in main copied its value to another "box", which we named 'a' in max. Notice, it's not the same "box" (variable) but it's the same value. We will explain it more clearly when we get to talk about passing variables the functions By Reference.
As you can see the main function above gets no inputs, but it can get arguments this way (we will not dwell on this now):
int main(int argc, char **argv) {
// code
// Success
return 0;
}
Moreover, our main doesn't return any output, and there its output type is void. In general, if we want to write a function which returns nothing we declare it with a void at the beginning. That means that void function can be very useful as a printing function or as a reading data from the user (or from a file) function. In the near future, we will learn how a function can also manipulate variables, without a need to return them (Hint: the By Reference thing again).
Notice that the main function in the last example above returns an integer as output. This is in accordance with the convention that if the main function finishes successfully it returns 0. It can also return something else according to what we decide. For example, negative numbers are usually returned when an error has occurred and we don't want to proceed.
Always remember, it's much C-mpler than you thought!
Regards,
Gilad
Posted on June 13, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.