Functions

Up until now, we have been working with programs where all of our code has been written inside of the int main() function. This is perfectly fine for small programs, but as our programs grow in scale and complexity, our main function becomes more difficult to debug and maintain. To help address this challenge, C has provided us with the ability to write functions.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Function declaration
void greeting();

// Function declaration and definition
int sum(int a, int b)
{
    return a + b;
}

int main(void)
{    
    int a;
    int b;
    int out;
    
    greeting();
    
    srand(time(0));
    
    a = rand() % 100;
    b = rand() % 100;
    
    out = sum(a,b);
    
    printf("%d + %d = %d\n", a, b, out);
    
    return 0;
}

// Function definition
void greeting()
{
    puts("Hello");
}

You will see this output, with some variation in the specific numbers:

Hello
33 + 87 = 120

The Anatomy of a Function

In C, a function can be generalized by the following template:

<return type> <function identifier>(<function parameters>)

To call a function, you use its identifier and then in the parentheses you pass 0 or more parameters. Once the function has finished, it will return a value matching the return type specified in the function declaration. If you are a math person, think of the parameters as the input and the return value as the output.

// Function declaration and definition
int sum(int a, int b)
{
    return a + b;
}

The sum function from our example is a good starting point to look at. In this instance, the return type is an int, and the function parameters are two int values. Note that you can have as many parameters as you want, as long as they have different names and are separated by commas.

out = sum(a,b);

We then call the function in our main function, and store its return value in the out variable, which has been declared an int to match the return value.

Declarations and Definitions

In order to call a function in main, it the compiler must be aware that the function exists before compiling the main function. In order to do this, we must declare the function. These declarations are sometimes called function prototypes.

// Function declaration
void greeting();

A function declaration is almost identical to a variable declaration, with the exception that it must also include parentheses and parameters.

// Function definition
void greeting()
{
    puts("Hello");
}

We then add the definition separately. It is not entirely clear why this is advantageous in our example, but imagine a function that is 500 lines. You will have to scroll over 500 lines just to get to your main function, which will make it a pain for anyone else who wants to look at your code. By using prototypes, you can make a nice list of function declarations above your main function, and then let anyone who wants to see the implementation details scroll down past your main function.

Pass by Value

If you have used Java or python, then you have used pass by reference functions. C is a pass by value language, so you will have to adjust some of your ideas to accommodate this difference. Consider the example below:

#include <stdio.h>

void increment_and_print(int num)
{
    printf("%d\n",++num);
}

int main()
{
    int a = 2;
    printf("%d\n",a);
    increment_and_print(a);
    printf("%d\n",a);
    return 0;
}

If you run this code, you will see the following output:

2
3
2

But why? Didn’t we increment our variable when we passed it to the function? The answer is not so simple. Yes, when we passed it to the function, it was incremented and printed. But there’s a catch. We only incremented and printed the value of the variable, we did not change the variable itself. Hence the phrase “pass by value”. There are ways to change variables you pass as parameters, but you will learn about that later.

<< prev | next >>

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x