Functions and Types in C Programming: A Comprehensive Guide.

Functions and Types in C Programming: A Comprehensive Guide.

Learn about functions, types, declaration, prototype, definition, calling and syntax

Introduction

Programming in general has something we call functions, now in particular, we will be looking at functions in C programming.

A Function is a block of code that performs a specific task. Functions help in breaking down large programs into smaller individual blocks of code, which makes the code easier to understand and maintain.

The function also helps us to be able to reuse a block of code. That is the same function can be used more than once in a code base to carry out the same task over and over again without writing it all over again from scratch.

Function Declaration

Before we can use a function in our C program, we need to declare it first, so that the compiler knows that we are about to use a function like that in our c program.

To declare a function, we simply need to take note of the following:

  1. The Return type

  2. The name of the function

  3. The Parameter type

  4. The parameter

Wherein the first, third and fourth are not necessary, the second one, which is the name of the function is very important.

So to declare let's say a function that will add two numbers together, we can do this;

int add_two_numbers(int x, int y);

From the code above, you can see where the first part of the function is the return type, which here we used int data type, using int means that the function will return an integer, although it's not always important for a function to have a return type, it could also return other data types like the char, the float etc.

Next, we have the name of the function, which is the add_two_numbers, this name must always be in place, and it is advisable to always use descriptive names for functions, that is they should always carry the name of what they do, as our function will be adding two numbers, so we named it add_two_numbers, which describes what the function does.

Next are our parameters, which are the x and y, along with their data type, which here is the int data type, also the parameters are not always important, as they can also be omitted depending on what the user wants. And also it is important that the parameter data types are also declared, or else it will return an error, and also for a function declaration, the parameters can be omitted, leaving only their data type, which here we can have something like this:

int add_two_numbers(int, int);

Always separate parameters with a comma. And note that the whole parameter can be omitted as well.

We will still see situations where the return type and the parameter can be omitted once we get to the type of function.

Then you end the declaration with a semi-colon.

Function Prototype

This is similar to function declaration, with just a small disparity. Where function declaration must be used in the file where the function is being defined.

The function prototype is often used in a file that is separate from the file where the function is defined and used.

The function prototype can also be inserted in the header file, which will be included in the source file that will carry the function.

Both the function declaration and prototype end with a semi-colon without the body of the function.

Function Definition

This is where the body of the function is been implemented. Here we not only include the return type, the function name and the parameter but also the block of code which is the implementation of what the function is expected to do.

In the function definition, you start with the return type, name of the function, parameter, and curly braces to house the codes that will be running under the function, and the codes themselves.

Let us take for example our above function which we declared, the definition is going to be:

int add_two_numbers(int x, int y)
{
    return (x + y);
}

From our function declaration, we now have a function definition that starts with a curly brace, what the function should do, is to add the two parameters together, and the result returned.

The function definition shouldn't always return something, it all depends on the kind of function we are dealing with, we will see more explanation of that under types of functions.

Note: Parameters are more like placeholders for the actual values which will be passed on to the function at the period of compilation.

When actual values are passed into the functions, they are called arguments.

Function Calling

It is not enough to declare and define a function, we will do all these at the initial stage so that we can be able to use the function whenever we need it.

That is where function calling comes into play, to use a function, we will have to call the function. And we call a function outside of the function and inside of where we want to execute the function. It can be in the main c function or any other function that is in our c program.

To call a function, we simply use the name of the function, followed by opening and closing braces, in a situation where the function is accepting inputs, that is parameters, we put the values as arguments into the braces each separated by a comma, in a situation where the parameter is more than a single value.

Below is a typical example of what I am talking about calling a function, let us call the function we have defined above:

add_two_numbers(3, 5);

This is how to call the function, just the name of the function and also the arguments, as you can see in the function declaration and definition, it has two parameters, which here is been replaced with an argument.

The result of this function call is going to be 3 + 5 which is 8.

More on function calls will be explained as we look at the types of functions we have.

Types of Functions

Unlike other programming languages, for C Programming language, there are about 4 types of functions, and why we need to make emphasis these 4 is because of the behavior of such functions, how they are handled and implemented all depends on what type they are;

Functions in C are divided into 4 types based on two major things: return type and parameter.

The return type is simply what the function will be returning after it finishes running.

The parameter is simply the input that is received by the function. Which could range from a single input to a multiple number of inputs.

On this note, we have 4 types of functions in C, and they are:

  1. Function without parameter and return type.

  2. Function without parameter but with the return type.

  3. Function with Parameter but no return type.

  4. Function with Parameter and return type.

Function Without Parameter and Return Type.

This function accepts no input and does not return anything. Here the return type is neither an int, a char nor a float, it simply does not have a return type, so for that, we apportion a void as the return type as we can not leave it as a space, so we will include void there to tell the compiler that it is not returning any value.

The same goes for the parameter also, it does not receive input and thus does not have any parameter. So we simply put a void there to let the compiler know that it is not receiving any input.

Below is an example of this type of function.

//function declaration
void add_two_numbers(void);

//function definition
void add_two_numbers(void)
{
    int x = 2, y = 8;
    x + y;
}

To call this function, and print the value out, we will have to use the main function in C.

#include <stdio.h>
//function declaration
void add_two_numbers(void);
int main(void)
{
    add_two_numbers();
    return (0);
}
void add_two_numbers(void)
{
    //declaration and initialization of variable x, y and res.
    //declaring variable res to store result of the sum
    int x = 2, y = 8, res;
    res = x + y;
    //print the result of the function
    printf("%d\n", res);
}

If you run and compile the code, you will get 10.

Function without Parameter but with Return Type

This type of function does not contain any parameter but has a return type, which could either be an int, a char, a float or any other data type in C.

Here the parameter will still be void, but the return type will carry the kind of data type that the function will be returning. Below is an example of this type of function.

//function declaration
int add_two_numbers(void);

//function definition
int add_two_numbers(void)
{
    int x = 2, y = 8;
    return (x + y);
}

Here, we used int as the return type, simply because we are expecting the function to return an integer, which in this case is going to be 2 + 8 which is 10.

Below is a full program to print out the result of such a function:

#include <stdio.h>
//function declaration
int add_two_numbers(void);
int main(void)
{
    //declaring variable to store result of the function
    int res;
    //function calling and stored into the res variable
    res = add_two_numbers();
    //print the result of the function
    printf("%d\n", res);

    return (0);
}
int add_two_numbers(void)
{
    //declaration and initialization of variable x and y
    int x = 2, y = 8;
    return (x + y);
}

In each of these programs, you can see that at the end of the main function, we always return a 0, this is because we signify that the main function is returning an integer, whose data type is an int. Thus, the value of zero is returned to mark the end of the program.

In a situation where the main return type is void, there won't be any need to return any value.

Function with Parameter but no Return Type

This third type of function in c has a parameter, but no return type, the number of parameters here can be a single value or numerous values separated by commas. The return type will simply be void.

Here is an example of such a type of function.

//function declaration
void add_two_numbers(int x, int y);

//function definition
void add_two_numbers(int x, int y)
{
    x + y;
}

Here, the function takes in two int data type parameters, which are x and y, add then adds them together.

Below is the full code:

#include <stdio.h>
//function declaration
void add_two_numbers(int x, int y);
int main(void)
{
    //function calling
    add_two_numbers(5, 7);

    return (0);
}
void add_two_numbers(int x, int y)
{
    //declaring the res variable to store result of the sum
    int res;
    // adding the two parameters and storing the result in res
    res = x + y;
    //print the result of the function
    printf("%d\n", res);
}

The calling of this third type is quite different from the first and second types as it doesn't require you to pass argument(s) into the function in the process of calling it, as in those two cases it does not have any parameter. Thus, it does not accept any argument too.

But in this third type, since it has parameters, which here are two, thus, in place of the parameters, we can simply pass our arguments, which are the actual value that will be replacing the parameters in the function as it is being called.

Here 12 will be returned as the function will take the first argument 5, and the second argument 7 and add them together, which will give us 12.

Function with Parameter and Return Type

The last type of function here has both a return type as well as accepts parameters, below is an example of this type of function:

//function declaration
int add_two_numbers(int x, int y);

//function definition
int add_two_numbers(int x, int y)
{
    return (x + y);
}

In this function, it has a return type of int, which tells that the function will return an integer type of data, then it has two parameters, x and y each of int data type.

Below is the full program for the function whose example we just explained:

#include <stdio.h>
//function declaration
int add_two_numbers(int x, int y);
int main(void)
{
    //declaring variable to store result of the function
    int res;
    //function calling and stored into the res variable
    res = add_two_numbers(5, 7);
    //print the result of the function
    printf("%d\n", res);

    return (0);
}
int add_two_numbers(int x, int y)
{
    return (x + y);
}

Here, the calling of the function is the same as of the third type, the only difference is that since this function has a return type, so we simply returned the addition of the two parameters, which are the x and y.

Conclusion

Although in this article, the focus was just on the int data type, that does not mean that a function return type and parameter can only have an int return type and parameter, we can also have a char return type and parameter, a float return type and parameter as well as any other valid data type in C programming language.

Also, remember that arguments are actual values that replace the parameters in the function, whereas parameters are like placeholders for the arguments to be passed into.

Thank you for reading. You can connect with me on Twitter and LinkedIn.