Functions
Preface
In this article we'll discuss functions. That's it.
Motivation
Sometimes you might want to reuse a segment of code elsewhere, and sure you can copy and paste it and then you're done, right? Well, for starters, if you find an error in the code, you'll have to fix it everywhere you copied it to, and there's the fact that shorter code is more legible, you want your code to be re-readable for other programmers, and mainly yourself. Plus, it looks neater to have a segment of code be labeled with one word.
Example - String Length
There's no way in C to figure out a string's length without knowing beforehand or counting every single char in the array apart from the null terminator "\0". If we're working with a large amount of strings, it would be useful to have a way to get the length by writing a single line of code, that refers to a different "place" in the code. Let's take a look at this function:
int strLen(char* str){
int count = 0;
while(str[count] != '\0'){
count++;
}
return count;
}
This function simply receives a string as an argument or parameter and iterates over each character of it, and returns an integer that is the number of characters before the null terminator, which is the length of the string. Sidenote, we aren't enforcing the fact that char* actually has a null terminator, it could also be an actual array of chars, In that case the compiler would be throw a segmentation fault. More on errors in a later article.
And to call the function all you have to do is:
strLen(aString);
Syntax
To use functions, you need declare and define them:
RETURN_TYPE FUNCTION_NAME(TYPE1 ARGUMENT1, TYPE2 ARGUMENT2, ..., TYPEN ARGUMENTN){
FUNCTION_BODY;
RETURN VARIABLE;
}
A function's signature, the first line, is how to declare a function. A function's signature must always begin with its return type, it can be any variable type, and it can also be void for no variable returned. If the function returns a variable, it must use the return keyword and return a variable from the type RETURN_TYPE, it can do so anywhere from within the function. You can also use return in void functions to exit the function without reaching the end.
After the return type, you must state the function's name, followed by a set of parentheses. Inside the parentheses, we have the function arguments or parameters, the variables that are passed to the function when called, you can have multiple parameters of varying types. Each argument must be declared just like a variable, with its type and name. If it receives no parameters, you can leave it empty, or prefarably, put the keyword void in it.
And inside the square brackets is where the function's body or definition.
Placement
In C, if you're calling a function, its definition must always preceed the call, for example, if you have a function call in main, and the function declaration and definition after main, the code will not compile. To solve this, you can write the functions signature before the call, at the top section of the file:
RETURN_TYPE FUNCTION_NAME(TYPE1 ARGUMENT1, TYPE2 ARGUMENT2, ..., TYPEN ARGUMENTN);
Notice the semicolon at the end.
Arguments/Parameter Passing
When you call a function and pass it arguments, the variables that are actually passed to the function are actually copies and aren't the actual variables. Well, how do we create functions that adjust the actual variables? And why if we pass an array it seems to adjust it's members just fine? We'll discuss all of that in the article about pointers which are very integral.
Summary
C functions are bare bones. No fancy function overloading or default parameter values. And just like most programming languages, when you pass them primitives, they are actually just copies. In next article we'll learn about pointers, and how the integrate into functions. Also for those who already know all about functions and pointers, you might be interested in article about function pointers! Sounds cool, right?