C Programming

How to Write a Function in C

At most, every programming language contains functions used to perform a specific task. Within the C language, the code is not a code without a function. Therefore, it must contain at least one method, i.e., main method. Now, we are ready to explain the syntax and a few associated examples of functions in the C programming language.

Syntax

Let’s have a look at the basic syntax of writing a function in C language. The first word, “return_type”, indicates the resultant value data type that this function will be returning to the main method. The “Function_Name” would be any name that will be given to a function. The parameter is used to pass some value to an integer that will be declared within this function body via the main method:

Main Method:

Let’s create a new file first. This file should contain a C extension. Hence, we have utilized the touch instruction in the shell to create the “test.c” file.

$ touch test.c

This file needs to be opened in an editor, such as GNU Nano Editor. Thus, we have been opening it to create a code in it using the “nano” keyword at the start of the query:

$ nano test.c

Within the C language, a code doesn’t work until the required header files are included. So, the most used header file is “stdio.h”, which has been included at the start of a code using a keyword “include” with a hash sign. It has been used for input-output stream purposes. First, we will be seeing how the main method can be defined in the C code. The main function may or may not contain a return type. In our example, we have been declaring the integer return type for the main method. Within the curly brackets, we have used the printf statement to display some messages on the screen. The “Ctrl+S” and “Ctrl+X” shortcuts can be used here to save and quit the file:

The C language code will be compiled by the “GCC” compiler. After successfully compiling, the code is executed, and the resultant output shows the display of some message:

$ gcc test.c
$ . /a.out

User-Defined Method:

To define the user-defined methods, you need to declare them within the code if you are defining them after the main method. Suppose we have to use the “Sum” method in the code. So, we have been declaring it first using the syntax shown in the picture after the header:

The defining of the user-defined function contains an overall implementation of the body of the function as well. The calculation of sum has been done in the method Sum. The user-defined methods don’t work without the main methods.

Therefore, we have been merging the function declaration, definition, and the main method within a single code file. The main method gets the two numbers at run time and saves them to the variables, n1 and n2. The “Sum” method has been passed by both the values by a function call.

It will calculate the sum of both numbers and return it to the main method to display it.

Compile first and run the updated code after saving it. The user will input the two integers, e.g., 5 and 8. The “Sum” method has calculated the total and returned it to the main method for displaying:

$ gcc test.c
$ . /a.out

Example 01:

Let’s create a new file to see how the built-in and user-defined methods can be created.

$ touch new.c

Within the file, we have added the input-output header file first. After that, the method “New” has been declared. The main method has been defined to call the method “New”. The function “New” has been defined after the main method here. It prints the original value of an integer “a” first then increments the original value by 3. At last, the updated value would be printed out:

The “new.c” file has been compiled after saving it using the “gcc” compiler. When we execute the file, the following outcome is as expected:

$ gcc new.c
$ . /a.out

Example 02:

Let’s revise the code a little. This time, we will be using the user-defined method “Max” to identify the maximum value from the given two integers. After the function declaration, the main method has been calling the “Max” method. The “Max” method checks the condition and returns the maximum value to the main method. Finally, the main method displays the following:

The execution of a file after the compilation returns the integer “89” as the maximum value:

$ gcc test.c
$ . /a.out

Conclusion:

This article is a full-fledged bundle of examples on how to write C functions. The examples contain the declarations and definitions of user-defined methods, implementations of main methods, and how to do function calls. By reading and understanding this article and its examples, I hope you can easily utilize functions in the C programming language.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content