How2Lab Logo
tech guide & how tos..


Functions in C - Part 1 of 5


C is a structured programming language. As mentioned in the introductory articles, a C program consists of a set of functions with one of them called the main(). Program execution always begins with the main function. Each function in C is a self contained program segment which works on one or more inputs that are passed to the function as parameters, and returns a value. Each function is essentially a block structure of C statements and enclosed within curly braces {}.

A function specification may contain a set of parameters. While using a function, it will be necessary to assign appropriate values to its parameters. The different type of parameter transfers between the calling and called functions will be discussed in detail later in subsequent articles.


Function definition and invocation

The syntax of function definition is as given below:

 <data type> function_name (argument 1, ..., argument n) 
 <type declaration of argument list>;
 {
	<statement 1>;
	<statement 2>;
	<statement 3>;
	...
	<statement n>;

	return(<expression>);     
 }

Here value of the expression in the return statement is returned to the calling program.

Example:

 /* Function to return maximum of two integers */
 int max(x, y)	//function definition
 int x, y;    	//type declaration of argument list
 {
	int c; 
	if(x > y)
		c = x; 
	else 
		c = y;
	return(c); 
 }

Note that you can combine the function declaration and argument type declaration. Thus, the above program can also be written as:

 /* Function to return maximum of two integers */
 int max(int x, int y)	//function definition & argument type declaration combined
 {
	int c; 
	if(x > y)
		c = x; 
	else 
		c = y;
	return(c); 
 }

In the above example, the parameters x and y are called the formal parameters of the function. The function max() can be called from another function with a statement such as:

 main() 
 {
	...
	int max_num, a=5, b=10;
	max_num = max(a, b);
	...
 }

When function max() is called, as shown above, the parameters a and b are called actual parameters. C binds the call to function max() using a scheme known as call by value. According to this scheme, the value of a is assigned to parameter x and that of b is assigned to y. Naturally a and b must be of type integers, like x and y.


Another Example:

 long int fact(n)
 int n; 
 { 
	int i; 
	long int prod=1;
	if(n > 1)
	{
		for(i=2; i<=n; ++i)
		{
			prod *= i;
		}
	}
	return(prod);
 }

 /* This function may be called in main as follows */ 
 #include <stdio.h>
 main()
 {
	int max(int,int);  //forward declaration of function to be called
	long int fact();   //forward declaration of the second function to be called
	int a, b, c; 
	long int z;

	scanf("%d %d", &a, &b);
	c = max(a,b);
	z = fact(c);

	printf("\nMaximum = %d\nFactorial of %d is %ld\n", c, c, z);

	/*
		You can also use a statement such as:
		printf("\nMaximum = %d\n", max(a,b));
	*/
 }

When a function (say main) wants to call another function (say max or fact), the latter should be declared in the calling function (viz. in main). Such a declaration is called forward declaration. In its simplest form a forward declaration can be written as<datatype> function_name();.

In a more comprehensive system, the data type of the parameters should also be specified. Such a declaration is called function prototype. Although the usage of the function prototype is not mandatory, it is desirable because such prototypes help in error checking between calls to the function and corresponding function definitions. Thus, in the above example, it would be better to declare fact aslong int fact(int);.

Note that if the function declaration precedes function call in your C code, then it is not necessary to include a function declaration within the calling portion of the program. So, in the above example, declaring function prototypes for max and fact in main is not mandatory. However, it is good practice to always declare function prototypes.


You may have a situation where a function is called but it does not return a value. In such a case, you may use void as data type for the return value. see example below:

 void maximum(int x, int y)
 {
	int z;
	z = (x >= y) ? x : y;
	printf("\nMaximum value = %d", z);
	return;
 }

You may also have a situation where a function does not need to accept any argument from the calling program. In such a case you may use void to define the argument data type. Example:void xyz(void). Here function xyz neither requires any parameter nor returns any value.



Exercise

For each of the following functions, write the first line of the function definition and its formal parameter declaration:

  1. The function test returns an integer data.
  2. A function named quadratic_polynomial accepts 4 floating point arguments and returns a floating point result.
  3. A function named after accepts a character and returns another character having next higher ASCII value.
  4. A function named rotate accepts 2 integer data and a floating point data as arguments and returns a character.
  5. A function named inverse accepts a character and returns its ASCII value (an integer data).
  6. A function named compute requires 2 floating point data and 1 integer data in order, and returns a double precision value.


Share:
Buy Domain & Hosting from a trusted company
Web Services Worldwide
About the Author
Rajeev Kumar
CEO, Computer Solutions
Jamshedpur, India

Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of premier engineering colleges & management institutes, and IT professionals.

Rajeev has founded Computer Solutions & Web Services Worldwide. He has hands-on experience of building variety of websites and business applications, that include - SaaS based erp & e-commerce systems, and cloud deployed operations management software for health-care, manufacturing and other industries.


Refer a friendSitemapDisclaimerPrivacy
Copyright © How2Lab.com. All rights reserved.