What is a function in the C.

C functions - basics

So far you are only familiar with mathematical functions with f (x) and do not really know how to proceed when setting up C functions? In this post you will learn to master them.

  • Structure of a C function - function head
    in the text
  • Structure of a C function - function body
    in the text
  • in the text

Structure of a C function - function head

A function consists of two parts, the head and the torso. Both are absolutely necessary for the function to be executable. It all starts with the functional head. It looks something like this: The type at the beginning is the function type that defines the return value of your function. The function name is actually self-explanatory. This is followed by the parameters in round brackets. You also have to specify them with their type and name.

But here, too, there are additional rules: Your return type must be one of the types provided by C, otherwise the program will not do anything. In addition, the names of your functions as well as your parameters may only contain letters - with the exception of umlauts - and underscores and must follow the other conventions.

Structure of a C function - function body

We come to the functional body. It consists of instructions enclosed in curly braces. Everything between these brackets will be explained later. However, in addition to this code, you can also use the values ​​of the transferred parameters.

The part between the brackets can contain many different types of commands.

On the one hand there are the comments, which have no effect on the program and are only written to provide a better overview for the developer, and on the other hand there are the remaining instructions.

These include variable agreements (int test), Arithmetic expressions (test + number) and function calls (startFunction ...), but also local integration of libraries (#include) and value assignments (test = ...). When things get a little more complicated, process structures such as conditions or repetitions are also used. Most functions conclude with the return value.

Call of the C function

So now we have defined our function. In order to be able to call it correctly in the main method, you have to pay attention to the type of head of the function when calling it. If there are parameters for the function to be called, they can either be hard-coded, i.e. written directly in the brackets, or soft-coded or referenced with variables. If you don't have any parameters that you have to specify, you can simply leave the brackets empty.

You should be aware that the call always goes hand in hand with the execution of the function and that it is really executed every time that is the case.

In this execution, the transferred values ​​are used in the body for the specified parameters and the instructions that you specified are executed in the order in which you wrote them. At the end, the function that called yours receives the calculated return value.

Now you know how a C function is basically structured and what you have to consider when calling it.