Functions in C Programming with example
A function is a module or block of program code which deals with particular task. Making C functions is a way of isolating one block of code from other independent blocks of code. Every C Program consists of one or more files, with each file containing zero or more functions, one of them being a main () function. Functions are defined as individual objects that cannot be nested.
Program execution begins with main (), which can call other functions, including library functions such as printf () and sqrt() Functions operate with program variables, and which of these variables is available at particular place in function is determined by scope rules.
Each function has a name or identifier by which is used to refer to it in a program. A function can accept a number of parameters or values which pass information from outside, and consists of a number of statements and declarations, enclosed by curly braces { }, which make up the doing part of the object. The declarations and ‘type of parameter’ statements are formalities which will be described in good time.
The name of function in C can be anything from a single letter to a long word. The name of a function must begin with an alphabetic letter or the underscore ‘_’ character.
Functions in C Example Code
Here is a real example function which adds together two integer numbers a and b and prints the result c.
Add_Two_Numbers (a,b) /* Add a and b */ int a,b; { int c; c = a + b; printf ("%d",c); }
Notice the position of the function name and where braces and semi-colons are placed: they are crucial. The details are quickly learned with practice and experience.
A function is called (i.e. control is passed to the function) by using its name with the usual brackets () to follow it, along with the values which are to be passed to the function:
main () { int c,d; c = 1; d = 53; Add_Two_Numbers (c,d); Add_Two_Numbers (1,2); }
The result of this program would be to print out the number 54 and then the number 3 and then stop. Here is a simple program which makes use of some functions in a playful way.
A function definition starts with the type of the function. If no value is returned, then the type is void. If the type is something other than void, then the value returned by the function will be converted, if necessary, to this type. The name of the function is followed by a parenthesized list of parameter declarations.
The function body is a block, or compound statement, and it too may contain declarations. Some examples of function definitions are
void nothing(void) { } double twice(double x) { return (2.0 * x); } int all_add(int a, int b) { int c; return (a + b + c); }
If a function definition does not specify the function type, then it is int by default.
For example, the last function definition could be given by
all_add(int a, int b) {
However, it is considered good programming practice to specify the function type explicitly.
There are several important reasons to write programs as collections of many C functions. It is simpler to correctly write a small function to do one job. Both the writing and debugging are made easier.