Table of ContentsC Programming - Functions (Part-I)
The concept of functions in C language is the same as for any other programming language. Few languages have methods but, since there are no classes in C, methods are not used.
Functions are best used for reusable code, or code that you know works and do want make use of it throughout development. If you find yourself tempted to use copy and paste on a particular block of code, even once, then you should move the code into a function.
Lets talk about the different types of variables before diving into functions as it is a bit of a prerequisite.
You can go forever without ever declaring a variable automatic, the reason being that all variables and functions are implicitly automatic. The technical meaning of automatic variable or function means that memory will automatically allocated for it by the compiler and will be destroyed automatically after it is no longer within scope.
Are exactly the same.
When programming in C, you should initialize the value of any variable before using it as early on as possible. It does not do this for you as many of the newer languages do.
As the name implies, external variables are variables declared someplace that would cause a compiler error when used if not for the extern keyword. You probably wonder why this is important, it will become apparent when we get to header files.
For now, know that there will be times when a variable needs to have a program-wide (global) scope and in order to allow other parts of the program to access it, it must be declared with the extern keyword.
This lets the compiler know that it is okay for other parts of the program to access this variable's value. You now just use it as you would any local variable.
This type of variable comes in very handy for embedded system coding where you want to reuse variables as much as possible for memory efficiency.
Here is a variable that no embedded application or operating system level application can live without. The static keyword in a variable's declaration means that the variable will be available throughout the lifetime of the entire application execution. So you can write interrupt driven code (common for system and embedded applications) without worrying that the variable will lose its current value or be unavailable if the program receives an interrupt and executes a different procedure that the one currently executing. When the interrupt procedure returns, your variable will still be available and have the value it had before the interrupt was called.
Imagine you have the following declaration in a program.
The first line declares a static integer for counting something. The interrupt function could now be called at any time throughout the execution of the program to increment the value of count. You can feel confident when calling the interrupt function again that it will have the same value as it did after the last time it was called. Of course this is assuming the only part of the program that ever changes the value is in that interrupt function.
There is another use for the static keyword in C. It keeps the variable local to the current source file. What this means for you is that you can not declare a variable as both extern and static. Large C programs are usually made up of many different source files and that is where this use for the static keyword comes in handy.
So this would not compile:
You must choose one or the other.
You could use the register keyword on variables that you think may be used often throughout your program. However, you are most likely wasting your time doing this because the compiler will ignore it if it thinks it can do a better job with optimization. It is probably right in thinking this too.
Register is a seldom used declaration, in fact I have never seen it in practice. I do not recommend trying to use it since compilers are pretty efficient these days. Knowing it exists is more than sufficient.
Functions are declared similar to regular variables.
As you can see, the only difference is the parenthesis after the name of the function. However, a declaration is not enough. A function must also have an implementation. To completely declare a function you have to have the following:
You can combine them by just having the implementation, but it is not good programming practice and would never be used in any professional code. But you should be aware because tutorials do it sometimes for brevity.