C programming is widespread and very powerful due to the easy in which a programmer can add new functionality. This tutorial explains how create a function using GCC.C has been a popular programming language for nearly 40 years. One reason for this is the fact that C is cross-platform and produces executables that are native to each operating system. Another reason is the ease with which the new programmer can get started with programming in C. However, the real power of C lies in the fact that a programmer can produce their own functionality quickly and easily without the need for complicated IDEs (Integrated Design Environments). They simple need a text editor and a C compiler for their operating system. They can then start creating their own C functions.The Main C FunctionEvery C program needs at least one function and it has a specify name. The one and only obligatory function must be called main:/*The main function requires no input and returns an integer*/
int main (void) {
/*This function is essential*/
return 0;
}
If this code is saved to a text file (for example simple_function.c) then the programmer may compile it:gcc -o simple_function simple_function.c
and then run it from the command line:simple_function
The program doesn’t do anything yet (apart from run the main function) but the programmer can quickly add their own functionality.Adding Functionality to the Main FunctionThe programmer can easily extend the main function. For example, they could include the stdio.h file which enables text to be written to the screen:#include ‹stdio.h›
int main (void) {
/*Output text to the screen*/
puts (“Hello, World”);
return 0;
}
If the program is compile and run now then the words “Hello, World” will be displayed on the screen. However, there is one drawback to placing all of the code in main. The function becomes very large very quickly and the reuse of code is difficult. The answer is to break the code down in to manageable and reusable functions.Why Use Function in a C Program?A programmer should always try to break their code in to blocks rather than having one massive function. This makes the code:easier to manage for both the programmers themselves and any other programmers that may have to work on the project;resuable, meaning that each block of code can be used more than once.There are two simple rules of thumb:each function should do just one job;the programmer should be able to view all of the code for a function on a single screen.If a programmers code violates either of these rules then they should consider dividing the code up into more functions.Adding a Function to a C ProgramThe main function shows the format for every C function. Each function consists of two elements:the function header which defines the return data type, the function name and any input variables;the function body – the code for the function.So, for example, one non-trivial function would be to output the sum of two numbers to the screen. It’s non-trivial because it involves converting the number to a string:void sum (int a, int b) {
/*Output sum of two numbers to the screen*/
int c = a + b;
int s;
char text sizeof(a);
s = sprintf (text, “%d”, c); /*”%d tell C that a decimal or integer is to be used*/
puts (text);
}
The main function can then be amended to run this function:int main (void) {
sum(13,24);
return 0;
}
However, the program is not ready to run quite yet.The Function PrototypeA function must be declared before it can be used. So, in the above example, and error will occur if the sum function is written after the main function. However, this potential problem is solved by using the function prototype. This is simply the function header copied to the start of the C file. So, the final code will look like:#include ‹stdio.h›
void sum (int a, int b);
int main (void) {
sum(13,24);
return 0;
}
void sum (int a, int b) {
/*Output sum of two numbers to the screen*/
int c = a + b;
int s;
char text sizeof(a);
s = sprintf (text, “%d”, c);
puts (text);
}
This new function can now be used as required within the application by the programmer.

Post Author: admin