¤ Home » Programming » C Tutorial » Functions in C – Part 1 of 5

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>;


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


 /* 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; 
		c = y;

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; 
		c = y;

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:

	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;

 /* This function may be called in main as follows */ 
 #include <stdio.h>
	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);

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.


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.


comments powered by Disqus

Download Mobile App for Android

Web Service Offers

.CLUB Registration
1,199 199 (83.4% off)
per Year

Ecommerce on the Cloud

An online store hosted on the Cloud. Sell unlimited products. Integrated business functions. Well-suited for high traffic website.

39,999 29,999 (25% off)

Ecommerce Premium

An e-commerce website well-suited for companies having a large portfolio of products. Sell unlimited products. Manage both offline and online sales from one system.

40,000 25,000 (37.5% off)

Ecommerce Business

Launch your online business. Sell up to 5000 products. Collect payment via multiple online and offline payment methods.

35,000 25,000 (28.57% off)

Ecommerce Pro

Get a Mobile friendly online store front. Sell up to 1000 products.

35,000 25,000 (28.57% off)

Ecommerce Starter

Start selling your products online. Scale up as your business grows.

35,000 25,000 (28.57% off)

Website on the Cloud

Custom built, responsive corporate website hosted on the cloud. Mirrored on 3 servers. Includes visitor signup with CRM functions/

19,999 9,999 (50% off)

Website Corporate

Custom built, responsive corporate website with CRM. Our most popular website plan.

19,000 9,000 (52.63% off)

Website Business - 1000 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

18,000 8,000 (55.56% off)

Website Business - 500 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,500 7,500 (57.14% off)

Website Business - 100 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,000 7,000 (58.82% off)

Website Business - 50 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,500 6,500 (60.61% off)

Website Business - 25 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,000 6,000 (62.5% off)

Website Starter - 5 Pages

Get started with your own website at most affordable cost.

15,000 5,000 (66.67% off)

Looking to build a website?
Launch a 5 page website in 1 day at only Rs.499/year. Hosting included.

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 XLRI, industry professionals, and govt. officials.

Rajeev has founded Computer Solutions & WebServicesWorldwide.com, and has hands-on experience of building variety of web applications and portals, that include - SAAS based ERP & e-commerce systems, independent B2B, B2C, Matrimonial & Job portals, and many more.

Copyright © How2Lab.com. All rights reserved.

Refer a friend | Sitemap | Disclaimer | Privacy Policy