Introduction to C functions

Introduction to C functions

Functions are the way we structure code into subroutines. We can:

  1. Give a name to
  2. Call when you need us

From your first program "Hello, World!", you immediately useCFeatures:

#include <stdio.h>

int main(void) {
	printf("Hello, World!");
}

Thismain()A function is a very important function because it is the entry point of a C program.

This is another function:

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

The function has 4 important aspects:

  1. They have a name, so we can call ("call") them later
  2. They specify a return value
  3. They can argue
  4. They have a body, wrapped in braces

The function body is a set of instructions that we execute every time we call a function.

If the function does not return a value, you can use keywordsvoidBefore the function name. Otherwise, you can specify the function return value type (intFor an integer,floatFor floating point values,const char *Used for strings, etc.).

You cannot return multiple values from a function.

A function can have parameters. They are optional. If they are not there, we insert in the bracketsvoid, like this:

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

In this case, when we call the function, we will call the function without adding any name in the brackets:

doSomething();

If there is a parameter, specify the type and name of the parameter, as shown below:

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

When calling the function, the parameter will be passed in parentheses, as shown below:

doSomething(3);

We can have multiple parameters. If so, we can use commas to separate them in the declaration and the call:

void doSomething(int value1, int value2) {
   /* ... */
}

doSomething(3, 4);

Parameter passingcopy. This means that if you modifyvalue1, Its value will be modified locally, and the value outside the function passed in the call will not change.

If you passpointerAs a parameter, you can modify the value of the variable because it can now be accessed directly using its memory address.

You cannot define default values for parameters. C++ can do this (Arduino language programs can also do it), but C cannot.

Make sure to define the function before calling it, otherwise the compiler will raise warnings and errors:

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

The warning you get regards the ordering, which I already mentioned.

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 doSomething(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 'doSomething' is invalid in C99
      [-Wimplicit-function-declaration]
  doSomething(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 doSomething(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.

Download my free C Handbook


More clang tutorials: