C Functions

A function is a group of code that performs a task together. Function in C language can be classified into two categories. They are:

  1. Library functions
  2. User-defined functions

The main is an example of a library function. There are some other library functions such as printf, scanf, sqrt etc. which are used in the program.
The main difference between library and user-defined is that you don't need to write library functions but you have to develop user defined functions at the time of writing a program.

There are three elements of a user-defined function. They are following:

  1. Function definition
  2. Function call
  3. Function declaration


Function Definition

A function definition is also known as function implementation. It is divided into two parts. They are function header and function body. The function header includes the following statements:

  1. Function name
  2. Function type
  3. Parameter list

The function body includes the following statements:

  1. Local variable declarations
  2. Function statements
  3. A return statement

The general form of function definition is given below:

Syntax

  1. function_type function_name(parameter list) {
  2. local variable declaration;
  3. statement 1;
  4. statement 2;
  5. ............
  6. return statement;
  7. }

An example of function definition is given below:

Example

  1. int add (int a, int b) {
  2. int sum;
  3. sum = a+b;
  4. return (sum);
  5. }


Function Call

You can call a function simply using the function name which is followed by a list of actual parameters. When a program calls a function the control is transferred to the called function.

The example of calling a function is given below:

Example

  1. main() {
  2. int x;
  3. statement 1;
  4. x = add(7, 5); /* Function call */
  5. printf("%d\n", x);
  6. }


Function declaration

In C programming all functions must be declared before they are invoked. A function declaration contains four parts. They are following:

  1. Function type
  2. Function name
  3. Parameter list
  4. A semicolon at the end

The format of function declaration is given below:

Syntax

  1. function_type function_name(parameter_list);

Some acceptable forms of function declaration are given below:

Example

  1. int add(int, int);
  2. int add(int a, int b);
  3. add(int, int);


An example of a program using user-defined function:

Example:

  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. /* function declaration */
  5. long long convert(int n);
  6. int main() {
  7. int n;
  8. cout << "Enter a decimal number: ";
  9. cin >> n;
  10. /* function call */
  11. cout << n << " in decimal = " << convert(n) << " in binary " << endl;
  12. return 0;
  13. }
  14. /* function definition */
  15. long long convert(int n) {
  16. long long bin = 0;
  17. /* local variable declaration */
  18. int rem, i = 1, step = 1;
  19. while (n != 0) {
  20. rem = n % 2;
  21. cout << "Step " << step++ << ":" << n << "/2, Remainder = " << rem << "Quotient = " << n/2 << endl;
  22. n /= 2;
  23. bin += rem * i;
  24. i *= 10;
  25. }
  26. return bin;
  27. }

Output:

Enter a decimal number: 19
Step 1: 19/2, Remainder = 1, Quotient = 9
Step 2: 9/2, Remainder = 1, Quotient = 4
Step 3: 4/2, Remainder = 0, Quotient = 2
Step 4: 2/2, Remainder = 0, Quotient = 1
Step 5: 1/2, Remainder = 1, Quotient = 0
19 in decimal = 10011 in binary