Function definition

I will explain the definition of the function. A function is a collection of functions. Describe the function name, argument name and its type and number, return type, and processing content.

// Function definition
Return type Function name (argument type 1 argument name 1, argument type 2 argument name 2, ...) {
  // Processing content
}

When calling the defined function, write as follows.

// Function call
Function name (value 1, value 2, ...);

Before calling a function, it is necessary to know the function name, the type and number of arguments, and the type of the return value by "defining the function" or "declaring the prototype of the function".

Function that outputs Hello World!

First, let's define a function with no arguments and no return value and call the function. If there is no argument, specify "void". If there is no return value, specify "void". Void is a special type that expresses that it does not exist. Note that if void is not written like "print_hello ()" in the argument part of the function definition, it means "arbitrary number of arguments of unknown type".

#include <stdio.h>

// Function definition
void myapp_print_hello (void) {
  printf("Hello World! \ N");
}

int main (void) {
  // Function call
  myapp_print_hello ();
}

This is the output result.

Hello World!

Function that adds two int32_t type numbers

Let's define a function that adds two int32_t numbers.

#include <stdio.h>
#include <stdint.h>

// Function definition
int32_t myapp_sum (int32_t num1, int32_t num2) {
  int32_t total = num1 + num2;
  return total;
}

int main (void) {
  // Function call
  int32_t num1 = 1;
  int32_t num2 = 5;
  int32_t total = myapp_sum (num1, num2);
  printf("%d\n", total);
}

Output result

6

Function to find the sum of dynamically generated arrays

It is a function to calculate the sum of array dynamically generated by calloc function. The point is to use pointer type as an argument.

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

// Function definition
int32_t myapp_sum_array (int32_t * nums, int32_t nums_length) {
  int32_t total = 0;
  for (int32_t i = 0; i <nums_length; i ++) {
    total + = nums [i];
  }
  return total;
}

int main (void) {
  // Function call
  int32_t nums_length = 3;
  int32_t * nums = calloc (sizeof (int32_t), nums_length);
  nums [0] = 1;
  nums [1] = 2;
  nums [2] = 3;
  int32_t total = myapp_sum_array (nums, nums_length);
  printf("%d\n", total);
  
  free (nums);
}

Output result.

6

Pass the contents of the structure as an argument

Let's write a function that changes the contents of the dynamically generated structure. The point is to define the argument as a pointer type.

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

// Structure definition
struct myapp_book {
  int32_t id;
  const char * name;
};;

// Function definition
void myapp_init_book (struct myapp_book * book) {
  book->id= 1;
  book->name= "C99 Tutorial";
}

int main (void) {
  struct myapp_book * book = calloc (sizeof (struct myapp_book), 1);
  
  // Function call
  myapp_init_book (book);
  
  printf("id:%d, name:%s\n", book->id, book->name);
  
  free (book);
}

Output result.

id: 1, name: C99 Tutorial

Function prototype declaration

We've covered function definitions above, but in practice there are more recommended ways to write them.

It is a split compilation method in which the prototype declaration of a function is made in the header and the function definition is made in the source code.

The prototype declaration of the function is written as follows. The argument name can be omitted. The return type, function name, and argument type should be the same as in the function definition.

// Function prototype declaration
Return type Function name (argument type 1 argument name 1, argument type 2 argument name 2, ...);

In C, the function definition must precede the function call.

If the function definition is after the function call, it's best to assume that you'll get a compilation error.

gcc implicitly resolves and issues a warning.

#include <stdio.h>
#include <stdint.h>

int main (void) {
  // Function call
  int32_t num1 = 1;
  int32_t num2 = 5;
  int32_t total = myapp_sum (num1, num2);
  printf("%d\n", total);
}

// The function definition is after the function call
int32_t myapp_sum (int32_t num1, int32_t num2) {
  int32_t total = num1 + num2;
  return total;
}

A warning is issued with gcc.

a.c: 8: warning: implicit declaration of function ‘myapp_sum’

Let's add a function prototype declaration before the function call. The warning will disappear.

#include <stdio.h>
#include <stdint.h>

// Function prototype declaration
int32_t myapp_sum (int32_t num1, int32_t num2);

int main (void) {
  // Function call
  int32_t num1 = 1;
  int32_t num2 = 5;
  int32_t total = myapp_sum (num1, num2);
  printf("%d\n", total);
}

// Function definition
int32_t myapp_sum (int32_t num1, int32_t num2) {
  int32_t total = num1 + num2;
  return total;
}

In the function prototype declaration, it seems better to omit the argument name and leave only the type and function name because it avoids macro expansion of the argument name.

#include <stdio.h>
#include <stdint.h>

// Function prototype declaration (better, but I can't just copy and paste ...)
int32_t myapp_sum (int32_t, int32_t);

int main (void) {
  // Function call
  int32_t num1 = 1;
  int32_t num2 = 5;
  int32_t total = myapp_sum (num1, num2);
  printf("%d\n", total);
}

// Function definition
int32_t myapp_sum (int32_t num1, int32_t num2) {
  int32_t total = num1 + num2;
  return total;
}

Example of split compilation

Here is an example of split compilation. When writing C language in practice, it is recommended to write in this way.

myapplib.h

This is a header file. Write a prototype declaration for the function. The "#ifndef MYAPPLIB_H" part is include guard.

#ifndef MYAPPLIB_H
#define MYAPPLIB_H

#include <stdint.h>

// Function prototype declaration
int32_t myapp_sum (int32_tnum1, int32_t num2);

#endif

myapplib.c

This is a C language source file with function definitions.

#include <stdint.h>

// Function definition
int32_t myapp_sum (int32_t num1, int32_t num2) {
   int32_t total = num1 + num2;
   return total;
}

myapp.c

The C language source code of the body of the application. Loading "myapplib.h".

#include <stdio.h>
#include <stdint.h>

#include "myapplib.h"

int main (void) {
   // Function call
   int32_t num1 = 1;
   int32_t num2 = 5;
   int32_t total = myapp_sum (num1, num2);
   printf("%d\n", total);
}

Compile, link and run

gcc -c myapplib.c
gcc -c myapp.c
gcc -o myapp myapp.o myapplib.o
./myapp

Output result.

6

Associated Information