Unit 7 Function
Unit 7 Function
In c, we can divide a large program into the basic building blocks known as function. The
function contains the set of programming statements enclosed by {}.
A function can be called multiple times to provide reusability and modularity to the C
program. In other words,
We can say that the collection of functions creates a program. The function is also known
as procedure or subroutine in other programming languages.
Advantage of functions in C
There are the following advantages of C functions.
o By using functions, we can avoid rewriting same logic/code again and again in a
program.
o We can call C functions any number of times in a program and from any place in a
program.
o We can track a large C program easily when it is divided into multiple functions.
o Reusability is the main achievement of C functions.
Function Aspects
There are three aspects of a C function.
Function Declaration
Function Call
Function Definition
Function call: Function can be called from anywhere in the program. The parameter list
must not differ in function calling and function declaration. We must pass the same
number of functions as it is declared in the function declaration.
Function definition: It contains the actual statements which are to be executed. It is the
most important aspect to which the control comes when the function is called. Here, we
must notice that only one value can be returned from the function.
Types of Functions
There are two types of functions in C programming:
1. Library Functions: are the functions which are declared in the C header files such
as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
2. User-defined functions: are the functions which are created by the C programmer,
so that he/she can use it many times.
#include<stdio.h>
void printName(); // function declaration
void main ()
{
printf("Hello ");
printName(); // function call
}
void printName() //function definition
{
printf("Birgunj");
}
Output
Hello Birgunj
Output
Enter two numbers
10 20
The sum is 30
#include<stdio.h>
int sum(int, int);
void main()
{
int a, b, result;
printf("\n Enter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("\n The sum is : %d", result);
}
C Library Functions
Library functions are the inbuilt function in C that are grouped and placed at a common
place called the library. Such functions are used to perform some specific operations.
Eg:- printf() is a library function used to print on the console. The library functions are
created by the designers of compilers.
All C library functions are defined inside the different header files saved with the
extension .h. We need to include these header files in our program to make use of the
library functions defined in such header files.
The list of mostly used header files is given in the following table.
Call by value and Call by reference in C
There are two methods to pass the data into the function in C language.
call by value
call by reference.
Call by value in C
In call by value method, the value of the actual parameters is copied into the
formal parameters.
In call by value method, we cannot modify the value of the actual parameter by
the formal parameter.
In call by value, different memory is allocated for actual and formal parameters
since the value of the actual parameter is copied into the formal parameter.
The actual parameter is the argument which is used in the function call whereas
formal parameter is the argument which is used in the function definition.
#include<stdio.h>
void swap(int x, int y);
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n", a,b);
}
void swap (int x, int y)
{
int temp;
temp = x;
x=y;
y=temp;
printf("After swapping values in function x = %d, y = %d\n", x, y);
}
Output
Call by reference in C
In call by reference, the address of the variable is passed into the function call
as the actual parameter.
The value of the actual parameters can be modified by changing the formal
parameters since the address of the actual parameters is passed.
In call by reference, the memory allocation is similar for both formal parameters
and actual parameters.
#include <stdio.h>
void swap(int *, int *);
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n", a,b);
swap(&a, &b);
printf("After swapping values in main a = %d, b = %d\n", a,b);
}
void swap (int *x, int *y)
{
int temp;
temp = *x;
*x=*y;
*y=temp;
printf("After swapping values in function x = %d, y = %d\n",*x,*y);
}
Output
Recursion in C
Recursion is the process of calling itself. Any function which calls itself is called recursive
function, and such function calls are called recursive calls.
Factorial = 120
scanf("%d", &n);
printf("Fibonacci series\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", Fibonacci(i));
i++;
}
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
#include<stdio.h>
void Bubble_Sort(int[]);
void main ()
{
int arr[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
Bubble_Sort(arr);
}
void Bubble_Sort(int a[]) //array a[] points to arr.
{
int i, j, temp;
for(i = 0; i<10; i++)
{
for(j = i+1; j<10; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
printf("Printing Sorted Element List ...\n");
for(i = 0; i<10; i++)
{
printf("%d\t",a[i]);
}
}
Output
7 9 10 12 23 23 34 44 78 144