Functions in C Language – A Brief Overview

Functions C Language Techhyme

C is probably the most widely known programming language. It is used as the reference language for computer science courses all over the world, and it’s probably the language that people learn the most in school/colleges among with Python and Java.

Also Read: Writing and Compiling a Simple C++ Program

C is not just what students use to learn programming. It’s not an academic language. And I would say it’s not the easiest language, because C is a rather low level programming language. Today, C is widely used in embedded devices, and it powers most of the Internet servers, which are built using Linux. The Linux kernel is built using C, and this also means that C powers the core of all Android
devices. We can say that C code runs a good portion of the entire world. Right now. Pretty remarkable.

Suggested Read: List of C Language Programs for Beginners

When it was created, C was considered a high level language, because it was portable across machines. Today we kind of give for granted that we can run a program written on a Mac on Windows or Linux, perhaps using Node.js or Python. Once upon a time, this was not the case at all. What C brought to the table was a language simple to implement, having a compiler that could be easily ported to different machines.

Functions

Functions are the way that you can structure your code into subroutines that you can:

  • give a name to
  • call when we need them

Starting from your very first program, an “Hello, World!”, you immediately make use of C functions:

#include <stdio.h>
int main(void) {
    printf("Hello, World!");
}

The main() function is a very important function, as it’s the entry point for a C program.

Here’s another function:

void example(int value) {
    printf("%u", value);
}

Functions have 4 important aspects:

1. they have a name, so we can invoke (“call”) them later
2. they specify a return value
3. they can have arguments
4. they have a body, wrapped in curly braces

The function body is the set of instructions that are executed any time we invoke a function. If the function has no return value, you can use the keyword void before the function name. Otherwise
you specify the function return value type (int for an integer, float for a floating point value, const char * for a string, etc).

You cannot return more than one value from a function. A function can have arguments. They are optional. If it does not have them, inside the parentheses we insert void , like this:

void example(void) {
    /* ... */
}

In this case, when we invoke the function we’ll call it with nothing in the parentheses:

example();

If we have one parameter, we specify the type and the name of the parameter, like this:

void example(int value) {
    /* ... */
}

When we invoke the function, we’ll pass that parameter in the parentheses, like this:

example(3);

We can have multiple parameters, and if so we separate them using a comma, both in the declaration and in the invocation:

void example(int value1, int value2) {
    /* ... */
}
example(3, 4);

Parameters are passed by copy. This means that if you modify value1 , its value is modified locally, and the value outside of the function, where it was passed in the invocation, does not change. If you pass a pointer as a parameter, you can modify that variable value because you can now access it directly using its memory address.

You can’t define a default value for a parameter. C++ can do that (and so Arduino Language programs can), but C can’t. Make sure you define the function before calling it, or the compiler will raise a warning and an error:

~ gcc hello.c -o hello; ./hello


hello.c:13:3: warning: implicit declaration of function 'example' is invalid in C99
[-Wimplicit-function-declaration]
example(3, 4);
^
hello.c:17:6: error: conflicting types for 'example' void example(int value1, char value2) {
^
hello.c:13:3: note: previous implicit declaration is here
example(3, 4);
^
1 warning and 1 error generated.

The error is about another thing, related. Since C does not “see” the function declaration before the invocation, it must make assumptions. And it assumes the function to return int . The function however returns void , hence the error.

If you change the function definition to:

int example(int value1, int value2) {
    printf("%d %d\n", value1, value2);
    return 1;
}

you’d just get the warning, and not the error:

~ gcc hello.c -o hello; ./hello
hello.c:14:3: warning: implicit declaration of function 'example' is invalid in C99
[-Wimplicit-function-declaration]
example(3, 4);
^
1 warning generated.

In any case, make sure you declare the function before using it. Either move the function up, or add the function prototype in a header file.

Inside a function, you can declare variables.

void example(int value) {
    int doubleValue = value * 2;
}

A variable is created at the point of invocation of the function, and is destroyed when the function ends, and it’s not visible from the outside. Inside a function, you can call the function itself. This
is called recursion and it’s something that offers peculiar opportunities.

You may also like:

Related Posts