Learning C – Part 2 Functions

What is a Function?

Functions serve as the foundation to almost any programming language. In our previous part we talked about Variables and Data types which will be critical in creating and using functions in C. For those who don’t know what a function is, it can be thought of as a sort of box. If something is put into the left side of the box then something can be taken from the right side of the box. The things we put into the left side of the box are called “Arguments” while what is taken out of the box is called the “Output”.

Functions can take any number of arguments so long as it is set up to do so. For example, a function can be made to only accept a single argument or it can be made to accept a varying number of arguments.Functions in C are made up of three parts: the return type, the function name, and the arguments. In some languages such as Python the type of output can be different things depending on the input the function is given. However, in C a function can only ever return a single type of output. If a function is designed to return an integer, it will always expect to return an integer.

The C Function Standard

The C function standard looks like the following:

The Function Declaration tells the program what the function’s name is, what type of data it will return, and what arguments it will accept. The Function Definition tells the program how this function will actually work. Just like in our box example in the first section, the Function Definition will make up our output using the arguments that are given to it.

The Quintessential C Program

The most common beginner C program is the ‘Hello World’ application. This usually consists of a single function called the Entry Point, an include to the standard Input/Output library, and a call to the printf function before returning 0. The return 0 in this case is also a C standard as 0 usually refers to a “successful run” and anything else is supposed to mean a crash or failure. In most cases the Entry Point to a C application will be int main() or int main(int argc, char* argv[]). The latter means that it accepts arguments to the program. This is just a standard for the C language and it may be different in certain cases.


Printf

The printf() function in this case is a C standard function from the <stdio.h> library, also known as Standard Input/Output. Printf will print any string to the console given an argument. As we mentioned before, every function needs to have a return type, name, and arguments. The function declaration for printf is int printf(const char* format, … );. The “…” represents variable arguments meaning that the function can take any number of arguments. If the declaration were int printf(const char* format); then printf would only be able to take a single string as its argument.

Variable Arguments and Format Strings

The variable arguments feature that printf shows us is incredibly useful as it allows us to string together arguments to make more complex calls. A programmer could do printf(“Hello World”); or printf(“Hello %s”, “World”); or even printf(“%s %s”, “Hello”, “World”);. The “%s” is called a format string specifier and it tells the printf function to put a string into that spot. The order is determined by the order of the variables after the first.


While variable arguments allow a programmer to put as many arguments as they want, format strings are how we tie them together in printf. C doesn’t care what kind of data is passed to these arguments, so it is important to be careful when printing or you will receive a bad output. A good example of this is printing the character ‘A’ as C has multiple ways of interpreting this character when it tries to handle the data.


Shown below are the various format string specifiers for printf. Some of these format string specifiers have little use in most applications such as %e, %g, %p, %o, and %n due to their nature.

Format Name Description
%s String Prints an array of characters (char *)
%d/%i Integer Prints an integer (base 10)
%c Character Prints a character
%x/%X Hexadecimal Prints a number in hexadecimal (base 16)
%n Number of Characters Prints nothing, the number of characters printed before it is stored in the integer given to it
%u Unsigned Integer Prints an unsigned integer
%o Octal number Prints an Octal number (Base 8)
%f/%F Floating point Prints a floating point (0.12345)
%e/%E Exponentiated Floating Point Prints a floating point number in scientific notation
%g/%G Shortest representation Prints the data in whatever form is shortest
%a/%A Hexadecimal Floating Point Prints a hexadecimal floating point value
%p Pointer Prints the address that a pointer points to

Making Our Own Functions

Now let’s make our own functions to practice. We’ll make an add and a multiply function. Both of these functions will take in two number (int) and return a number as a result (int). Functions have to be declared above where they are used. This means that the code for our function either has to be above our main function as shown in the code sample below, or we have to do a declaration above our main function. A declaration in this case is just the normal declaration of the functions with a semi-colon (;) instead of braces ({}) such as:
int add(int a, int b);
int multiply(int a, int b);
This tells the compiler to go fetch the code for our function as if we placed it in this location.

Both of these functions are pretty simple, but an experienced programmer can write vastly more complicated functions and even tied them together to perform large operations with ease.

Next >>