2020
Functions (C)

Functions (C)

Translated from German using DeepL.

Date: February 2020
Reading time: 4 minutes


When you program something, you don't write the whole code in one instruction block, but break the program down into several subroutines (functions).

In this blog post, I explain the functions using C. However, this is not the only programming language in which you can make use of functions. The syntax differs from language to language.

Anyone who has ever written a C program has already seen a function. The main function. All functions are called from the main function.

#include <stdio.h>
#include <stdlib.h>
 
int main() {
	printf("Hello world!\n");
	return 0;
}

Also printf(), scanf(), etc, are functions which we can call.

What are functions?

What are functions? What do you need them for?
Functions can make your life easier.For example, if I want to output "Hello World" several times, I always have to output the text separately. This neither looks nice nor is it maintainable. If I now have to change the text to "Hello World", I have to rewrite every single printf command. This is tedious and takes a lot of time.

These problems can be avoided with functions. If you write the output of the text in a function, you only have to call it. The text is then output each time it is called. Once you have created a function, you only have to change the text once.

Functions therefore have many advantages. They make the code easier to read, understand and maintain.

The program would then look like this:
(Now it's all about understanding. The exact syntax will be explained below.)

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

The function begins in line four. "Hello World" is programmed in line five. In the ninth line, the function is called with hello(). Only now are the commands in the function executed. Without hello() there would be nothing in the console.

Syntax

When writing functions, you must pay attention to whether the function can receive or return something. You must not be confused by the return value. printf() does not return a value, but outputs one.

A standard function is defined as follows.

<Datentyp> <Funktionsname>([<Parameter>]) {}

A data type must be specified at the beginning if the function is to return something. This is followed by the function name. You can choose this yourself. Finally, you can specify one or more parameters. These are only necessary if you want to pass something to the function. The data type and parameters can be replaced with void if they are not required.

Call

A function is called as follows:

<Function-Name>([<Parameter>]);

Types

There are four types of functions:

Function typeReturn valueParameterCode example
function without return, without parametersNoneNonevoid func1() { printf("Hello world!""); }
Function without return, with parametersNone1 or morevoid func2(int x) { printf("Value: %d", x); }
function with return, without parametersYesNoneint func3() { return 42; }
Function with return, with parametersYes1 or moreint func4(int a, int b) { return a + b; }

Pre-declaration

If the function is unknown when the program is executed, this may be because it was not defined before the call. The application is executed from top to bottom. The function declaration must therefore always be made before the call.

Example

#include <stdio.h>
#include <stdlib.h>
 
float readNumber() {
	int number;
	printf("Please enter a number: ");
	scanf("%d", &number);
	return number;
}
 
void printResult(int res) {
	printf("The result is: %d", res);
}
 
void printGraph(int amount) {
	int count = 0;
	while (count < amount) {
		printf("*");
		count++;
	}
}
 
int main() {
	printf("Functions\n");
	printf("----------------\n");
 
	int sum = readNumber() + readNumber();
	printf("----------------\n");
 
	printResult(sum);
	printf("\n");
 
	printGraph(sum);
	printf("\n");
 
	return 0;
}

This program reads in two numbers and then adds them together. It also outputs the number of the sum as an asterisk.

It has three functions:

  • readNumber(): Asks for a number, stores it in a variable and returns the result.
  • printResult(int res): Outputs the result that must be passed when the function is called.
  • printGraph(int amount): Draws a number of asterisks on the console corresponding to the result.

In the main function, "Functions" is output first and underlined.
Then the function readNumber() is called twice. This function reads in the numbers and returns the read-in value with return. The two values are added together and written to the variable sum. The function printResult is then called. The parameter sum (the sum of the values entered) is passed. The function now outputs the result contained in sum.
Finally, printGraph is executed. Here, too, sum is passed. Now asterisks are written to the console until count is the same size as the sum.