c programming functions tutorial

C Programming Functions Pt/1

C programming functions Part 1, will be a basic introduction to functions in the C language.

A function is an independent part of the program, and can perform a specific tasks without interference from other parts of the program.  Functions can be passed parameters and can also return values.

There are 2 types of functions in C, the existing standard library functions i.e. printf, scanf, gets etc, and user defined functions, that are written for specific tasks by the user.

A user function when used in C programming, should first have a function prototype, then a function definition and then there is the function call inside of the main function.  The image below shows a basic program with a user defined function, illustrating a function prototype, function definition and function call.

C Programming Functions Tutorial Eclipse

The function prototype provides the compiler with information about the function, that will be defined later in the function definition.  The prototype contains the return type, the function name and any parameters with variable types the function is passed.  The function prototype ends with a semicolon (;).  The function prototype is usually included before the main function.

The function definition is the actual function.  The first line of the function definition is identical to the function prototype i.e. return type, the function name and any parameters. Additionally the function definition contains all the code that the function will execute, i.e. statements etc.  The function definition unlike the function prototype does not end with a semicolon, but uses curly brackets or braces ({ }), these surround the code that forms the function body.  The function definition can be placed before the main function, but it is often good practice to place it after.

The function call is used to execute the function, and is run from within the main function.

Defining A Function

A function definition in C programming consists of a function header and a function body.

return_type function_name (parameters)  // Function Header
{
      statements;  // Function Body
}

return_Type – A function may return a value, the return type is the data type of the value the function returns i.e. int, char etc.  A function can also not return a value, in this case the return type would simply be the keyword void.

function_name – The function name is the unique name for the function chosen by the programmer.  There are certain rules that have to abide by for the C language.

parameters – When a function is called, a value is passed to the parameters.  A function needs to know what data type the arguments will be, any of C’s data types can be passed to a function.  If the function takes no parameters then the keyword void is used to indicate this.

C Programming Functions Tutorial Eclipse

Simple Function Examples

For all these examples Eclipse Kepler was used, there is a small downloadable zip file below with all the full examples including includes etc.

Example 1

int a = 10, b = 5, c;
int d = 5, e = 5, f;

int product(int x, int y);

int main(void)
{
	c = product(a,b);
	f = product(d,e);

	printf("%i\n",c);
	printf("%i",f);

	return 0;
}

int product(int x, int y)
{
	return (x * y);
}

First lets break down the entire code by line number:  Lines 1 and 2 contain global variables, variables a, b, d and e are all initialized with values.  Line 4 is the function prototype.  The main function starts on line 6 and finishes on line 15.  Line 8 and 9 are both function calls, for the same function but are passed different arguments.  Also line assigns it’s returned value to the variable c, and line assigns it’s return value to the variable f.  Lines 11 and 12 call a C library function called printf, this is used to firstly print the value of the variable c on the screen, return a line and then print the value of f on the screen.  Line 14 is required and in this case the main functions parameter is void, so nothing is returned, hence the zero. Line 17 is the start of the function definition, and also contains the function header.  Line 18 to 20 is the function body and the end of the function definition.

So what does this simple function do?  Well when the program is run line 8 is called first, this executes the product function using variables a and b as arguments.  In the function definition (and prototype) x and y can be looked at as place holders.  So when variables a and b are passed to the function, a is placed where x is and b is placed where y is.  Then the function simply multiplies the 2 arguments together and returns the result.  The result is then assigned to the variable c.

The same is true for the function call in line 9, variables with a different name and value are used, but otherwise it is the same sequence of events.  After this lines 11 and 12 are called and the values of c and f are printed on the screen.  So the result will be c = a*b therefore 50 = 10*5 and f = d*e therefore 25 = 5*5.

Example 2

void function_1(int x);
int function_2(void);

int main(void)
{
	function_1(function_2());

	return 0;
}

int function_2(void)
{
	int a = 10;

	return a;
}

void function_1(int x)
{
	int b = 10, c = 5, result;
	result = x * b /c;

	printf("%i",result);
}

First lets break down the entire code by line number, as with the previous example:  Line 1  contains the first function prototype, this function does not return a value, but accepts 1 parameter.  Line 2 contains the second function prototype, this function returns a value in the form of an integer, but does not accept any parameters.   The main function starts on line 4 and finishes on line 9.  Line 11 to 16 is the function definition for the first function. Line 18 to 24 is the function definition for the second function.

So whats going here?  Well when the main function is called, Line 6 is reached and the first function is called (function_1).  The function_1 is defined as not returning any value, but it accepts 1 parameter in the data type of an integer.  The function_2 is defined as returning a value in the data type of an integer, but not taking any parameters.  So when function_1 is called it then calls function_2, and the return value from function_2 becomes the parameter or argument passed to function_1.  The return value of function_2 is very simple in this case, and just equates to the variable a which has a value of 10.  So the variable a is passed to function_1, using the place holder analogy this value becomes the variable x.  There are 3 local variables inside function_1, these are b (10), c (5) and result.  Then x is multiplied with b and divided by c, the result of the calculation is then assigned to the variable result.  Finally the value of result is printed on the screen using the printf function.

So the final outcome of this program will be resultx*b/c therefore 20 = 10*10/5.

Examples Code

The link below contains the zip file with the complete C code.

Function example code

Leave a Reply