In C, most of the code you'll execute is inside functions. Think of functions as some processes that happen when a program runs. Keeping that analogy of a process or a routine, it is important to note that the total of these processes constitutes a task done. For example, a conveyor belt moving bottles is a process that feeds into another process e.g. of the bottles being stored completing a manufacturing task. In C programming, functions are these processes. they are essentially lines of code that perform a particular task. Say you wish to add to numbers, so you call a function(process) that adds two numbers, give it two numbers and it returns the sum to you. That's all functions are, lines of code that do a particular task.
Return values and arguments.
When a function completes a task, it could return a value to the calling function or process. This value is usually a way to tell something happened. It could indicate the task was performed successfully or an error occurred and the task was not finished among other things. These return values are important when calling and using functions. For example, a function to count the number of characters in a string could return the number of characters it counted. A function to write to the standard output could return the number of characters it printed or (-1) to indicate an error occurred and a character was not printed. All these values are important to the caller of the functions and help decide what to do next.
Another concept when dealing with functions is the concept of arguments. The function could use arguments to perform its task. Arguments are like the tools it needs to perform these tasks. The argument to a function in C, is usually passed in brackets after the function. Say the function is to count the number of characters in a string, it could take the string as an argument. This could be written as:
int _strlen(char *str);
In this case, the str
variable represents a string that the function will take and use. In case a function needs more arguments, they are separated by a comma. e.g. int _strnlen(char *str, int n);
. In this updated function, the function takes a string and an integer as its arguments. The integer could be anything, in this case, the function could be counting the characters but it would not exceed a given number, n.
The int
before the function name gives its return type. In this case, it returns an integer but it could be returning any different type as needed by the user.
Using functions.
To use a function in C, it must be declared first. The prototype must be declared and the function defined. A definition of a function consists of lines of code that make up the function.
Function prototypes.
The function return type, name and the arguments it takes constitute its prototype. The prototype for the string length function above would be, int _strlen(char *str);
. Once its prototype has been declared, it can then be called and used after being defined in the program.
Function definitions.
The definition of a function is the lines of code inside it that execute the task. The above function could be defined as:
int _strlen(char *str)
{
int count = 0;
while (str[count])
count++;
return (count);
}
Note the return statement precedes the return value. This code defines the function and thus the function can be called elsewhere.
This has been a fun introduction to functions in C, we'll continue from there on how to use them and call them. We'll continue from this point as we delve more into the awesome world of C.