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:
- Library functions
- 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:
- Function definition
- Function call
- 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:
- Function name
- Function type
- Parameter list
The function body includes the following statements:
- Local variable declarations
- Function statements
- A return statement
The general form of function definition is given below:
Syntax
function_type function_name(parameter list) {local variable declaration;statement 1;statement 2;............return statement;}
An example of function definition is given below:
Example
int add (int a, int b) {int sum;sum = a+b;return (sum);}
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
main() {int x;statement 1;x = add(7, 5); /* Function call */printf("%d\n", x);}
Function declaration
In C programming all functions must be declared before they are invoked. A function declaration contains four parts. They are following:
- Function type
- Function name
- Parameter list
- A semicolon at the end
The format of function declaration is given below:
Syntax
function_type function_name(parameter_list);
Some acceptable forms of function declaration are given below:
Example
int add(int, int);int add(int a, int b);add(int, int);
An example of a program using user-defined function:
Example:
#include <iostream>#include <cmath>using namespace std;/* function declaration */long long convert(int n);int main() {int n;cout << "Enter a decimal number: ";cin >> n;/* function call */cout << n << " in decimal = " << convert(n) << " in binary " << endl;return 0;}/* function definition */long long convert(int n) {long long bin = 0;/* local variable declaration */int rem, i = 1, step = 1;while (n != 0) {rem = n % 2;cout << "Step " << step++ << ":" << n << "/2, Remainder = " << rem << "Quotient = " << n/2 << endl;n /= 2;bin += rem * i;i *= 10;}return bin;}
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