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 <math.h>
  2. #include <stdio.h>
  3. long long convert(int n);
  4. int main() {
  5. int n;
  6. printf("Enter a decimal number: ");
  7. scanf("%d", &n);
  8. printf("%d in decimal = %lld in binary", n, convert(n));
  9. return 0;
  10. }
  11. long long convert(int n) {
  12. long long bin = 0;
  13. int rem, i = 1, step = 1;
  14. while (n != 0) {
  15. rem = n % 2;
  16. printf("Step %d: %d/2, Remainder = %d, Quotient = %d\n", step++, n, rem, n / 2);
  17. n /= 2;
  18. bin += rem * i;
  19. i *= 10;
  20. }
  21. return bin;
  22. }

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