C function basics for beginners

C function basics for beginners

C is a vast language. From school itself, students love the essence of C programming. C is known as the mother of all languages. Quite apt as I would say, because C is a vast language and the scope of its utilities are yet more. In this article, I would give some very basic tips on handling custom functions or user-defined functions in C. C function basics is very important for a programmer in the long run. Without functions a code can be as bad as a car without wheels. Functions are therefore a crucial part.

Several students and beginners have a strange fear for functions. C function basics will however change this idea. You will actually know that making custom functions is very easy and they reduce a huge lot of pressure from the developer.

C function basics – the prelude

First of all, let me define what functions actually do for you if you are a developer. You might have come across several instances where you require writing the same code over and over again in the same program. Suppose, you are writing a menu driven calculating program and you need to find average of some numbers and their percentage. In both cases you would require to find the sum of all the numbers. Here you have two possibilities; either you write down the addition code twice at two different places or you can write a function for it and just call it twice. Obviously the second option feels much better to hear. Well, C function basics will also enable you to feel great when follow the second option.

Starting a function syntax with C function basics

For functions, the C compiler looks for three basics things – the function prototype, function declaration or definition and the function call. The function prototype tells the compiler what the function is about. So, it has a name, a return type and the argument list. The name can be any alphanumeric word, starting with a small letter and outside the token list of the C language. The return type is the data type of the function’s return value. If this is defined void, then the function will return no value. Otherwise, the function returns a value of the mentioned data type. The argument list is the list of values the function is supplied with. So, a proper function prototype looks like the following

/* First the return type, then the function name and then within the 
   braces - the argument list */

int sum (int a, int b);

The next part is the function definition. The function definition syntax is easy. Just copy the prototype line and start off with curly braces. Within the curly braces, the function’s work is defined. So, for example, the addition function will collect arguments to be added and return the sum. This calculation will be defined inside the function body.

int sum (int a, int b)
int c = a + b;
return c;

Finally, the last part of C function basics is the function call. So, in this case, whenever you need to add numbers just make a call with the function name and supply the numbers to be added as the arguments. Make a note here that functions with return types always return a value. So either print the function call directly or store it in a variable to get the returned value.

// Either print directly...
printf("%d", sum(5, 10));

// Or you can store the result...
int x = sum(5, 10);

C function basics will help you grasp a lot about the function programs. Just try out simple examples of this and soon you would master the art.

Comments are closed.