Function
Function is a block of code which only runs when it is called. Every C program has at least one function which is main function with we can divide code into separate function. A function deceleration tells the compiler about of function name, return type parameter list and terminate by semicolon. A function definition provides the actual body of the function. A function can be referred as a method which is also called function call/ called.
Eg:
#include <stdio.h>
int factorial(int n);
int main()
{
int num;
printf(“Enter a positive integer: “);
scanf(“%d”, &num);
printf(“Factorial of %d = %d\n”, num, factorial(num));
return 0;
}
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n – 1);
}
Function Aspects:
- Function declaration:
- Function definition:
- Function call:
Declaration of function
A declaration of function is also referred to as a function prototype. The declaration of function consists of these three componts:
- Return type,
- Function name,
- Parameter list
Syntax:
Return_type function_name(type1 para1, type2 para2………, type n paran);
Or
Return_type function_name(type1, type2,………., type n);
Definition of function
Function definition is a block of statements that describes the specific task to be done by the function. It consist of two main components:
- Function header
- Body of the function
Function header defines the name of the function, its return type, and its arguments list similar to the prototype but without (;) terminating the line.
Function body contains a block of code enclosed in a parenthesis.
Syntax:
Return_type function_name(data_type1 var1, data_type2 var2,……..data_type n var n)
{
//statements;
}
Calling of function
When a function is called in a program, the block of statements in a function definition executes. A function is called or accessed by the function name followed by arguments list.
Syntax:
Variable_name= function_name(arg1, arg2,……..,arg n);
Types of function:
- Library function:
Those function which are already defined in C library.
Eg : printf(), scanf(), getch() etc. it is also called predefine function.
Program:
#include<stdio.h>
#include<math.h>
Void main()
{
Int a= 25, p;
P= sqrt(a);
Printf(“the square root is %d”,p);
}
- User defined function:
Those function which are defined by user at the time of writing the program is called user defined
function .
Eg: sum(), product(), division(), add(), sub() etc.
Program:
#include<stdio.h>
Int add(int a, int b);
Void main()
{
Int a, b, x;
Printf(“enter two numbers:”);
Scanf(“%d %d”, &a, &b);
X= product(a, b);
Printf(“the product is %d”, x);
}
Int product(int a, int b)
{
Int p;
P= a*b;
Return p;
}
Benefits of using functions:
- It can be developed in short period of time using function.
- It can be used in minimization of code.
- It makes you code reusable. You have to call the function by its name to use it; whenever it required.
- In case of large program with thousand code of line, debugging and editing becomes easier.
- It makes the program more readable and easier to understand.
Categorizes of function
Function can be categorizes into four types on the basis of whether Argument are present or not and weather the value is return or not.
- Function with arguments and return type:
The argument is passing from the calling function to the called function and the value is also returned from called function to the calling function.
Eg:
#include<stdio.h>
Int sum(int, int);
Void main()
{
Int a=2, b=3, c;
C= sum(a+ b)
Printf(“the sum of two numbers is %d”,c);
Getch();
}
Int sum(int x, int y)
}
- Function with argument and no return type:
The argument is passed from the calling function to the called function but the value is not returned from called function to the calling function.
Eg:
#include<stdio.h>
Void add(int, int);
Void main()
{
Int a=50, b=90;
Add(a, b);
}
Add(int a, int b)
{
Int sum;
Sum= a +b;
Printf(“the sum is %d”,sum);
}
- Function with no arguments and return type:
The argument is not passed from the calling function to the called function but the value is returned from called function calling function.
Eg:
#include<stdio.h>
Int add();
Void main()
{
Int x;
Printf(“the sum is %d”, x);
}
Void add()
{
Int a=50, b=90, sum;
Sum= a+ b;
Return sum;
}
- Function with no argument and no return type:
The argument is not passed from the calling function to the called function and also the value is not returned from called function to the calling function.
Eg:
#include<stdio.h>
Void add();
Void main()
{
Add;
}
Void add()
{
Int sum, a +b;
Sum= a+ b;
Printf(“the sum is %d”, sum);
}
Difference between library function and user-defined function
Library function | User-define function |
Library functions are predefined function. | User-defined functions are not pre-defined and |
need to be defined by the programmer. | |
Header file should be included to use it. | Function prototype is required to use it. |
Since, these functions are predefined programs | Since, these functions should be defined by the |
are short. | user, programs are lengthy. |
Program development time will be faster. | Program development time will be usually |
slower. | |
It is called at run time. | It is called compile time. |
The name of library functions cant be changed, | The naming of user-defined functions is users |
choice | |
Eg: printf(), scanf(), strlrn(), etc. | Eg: product(), sum(),reverse(), etc. |
Void statement:
There can be some situations where we cannot return value to a calling function. In such case as a return type we can use ‘void’. The functions return type is void if function doesn’t return a value to a calling function.
Eg: program to display a series 2 4 6 8 ….. 10th terms.
#include<stdio.h>
Void series(int);
Void main()
{
int a=2;
Series(a);
}
Void series(int a)
{
Int i;
For(i=1; i<=10; i++)
{
Printf(“%d\t”, a);
A=a+2;
}
}
Type of function call:
- Call by value
- Call by reference
There are two methods to pass the data into function in c language that is function call by value and function call by reference:
A’ function call by value:
In function call by value method, the value of actual parameter is copied into the formal parameter. Different memory is allocated for actual and formal parameter since, the value of actual parameter is copied into the formal parameter. The actual parameter is the argument which is used in function call whereas the formal parameter is the argument which is used in function definition.
Syntax:
Void fun(int, int);
{
}
Fun(x, y);
Program: #include <stdio.h> void swap(int x, int y); int main() {
int a = 10; int b = 20;
printf(“Before swapping, a = %d and b = %d\n”, a, b); swap(a, b);
printf(“After swapping, a = %d and b = %d\n”, a, b);
return 0;
}
void swap(int x, int y) {
int temp;
temp = x;
x = y;
y = temp;
printf(“Inside the swap function, x = %d and y = %d\n”, x, y);
}
B’ function call by reference:
In function call by reference method, the address of variable is passed into the function as actual parameter. The value of actual parameter can be modify by changing the formal parameter. The memory allocation is similar for both formal and actual parameter.
Syntax:
Void fun(int*)
Eg:
#include <stdio.h>
void swap(int *a, int *b);
int main() {
int num1, num2;
printf(“Enter two numbers: “);
scanf(“%d %d”, &num1, &num2);
printf(“Before swapping: num1 = %d, num2 = %d\n”, num1, num2); swap(&num1, &num2);
printf(“After swapping: num1 = %d, num2 = %d\n”, num1, num2); return 0;
}
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
Recursion function
Function which call itself until the specific condition has been satisfied is called recursion function and the process in which function call, itself is called recursion function. It is also called the recursive function. To solve the problem with recursion function, the following conditions should exist:
- The function should call itself
- It should have a stopping condition to stop the further recursive calls.
Syntax:
Void recurse()
{
recurse();
}
Eg:
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n – 1);
}
int main() {
int num;
printf(“Enter a number to find its factorial: “);
scanf(“%d”, &num);
int fact = factorial(num);
printf(“Factorial of %d = %d\n”, num, fact);
return 0;
}
Advantage of a function
Following are the advantage of a function:
- Function improves code reusability and avoid rewriting same lines of code again and again in a program.
- The size of program i.e. the length of program can be reduced by using function.
- Many programmers can work together in a program development which speeds up the program development time.
- The program decomposed into well-defined functions gives more logical clarity and is easier to understand.
- Function can be called number of times in a program.
- The debugging of program becomes easier.
- The programs can be tested and compiled independently by different members of a team which can be later integrated to a single unit.