C Language | Functions | Creating Functions

Repeated processing can be organized into reusable functions.

Organizing Processing into a Function

main() is the application’s entry point, but programs can define additional functions.

return_type function_name(parameter_list) {
  statements
}

A function that neither receives nor returns a value can be written with void.

#include <stdio.h>

void Function(void) {
  printf("Kitty on your lap\n");
}

int main(void) {
  Function();
  Function();
  return 0;
}

Define or declare a function before calling it so that the compiler can verify its return type and parameters. Calling an undeclared function is obsolete syntax and is not allowed by current C standards.

When a function finishes, control returns to its caller. A void function may omit return, or use return; to finish early.

void Function(void) {
  printf("Kitty on your lap\n");
  return;
}

Function calls can be nested.

#include <stdio.h>

void Function2(void) {
  printf("Function2() : return\n");
}

void Function1(void) {
  printf("Function1() : Call Function2()\n");
  Function2();
  printf("Function1() : return\n");
}

int main(void) {
  printf("main() : Call Function1()\n");
  Function1();
  printf("main() : return\n");
  return 0;
}

Each function returns control to the place that called it, eventually returning to main().