Function in Detail 11
Function in Detail 11
The function is a self contained block of statements which performs a coherent task of a same
kind.
C program does not execute the functions directly. It is required to invoke or call that functions.
When a function is called in a program then program control goes to the function body. Then, it
executes the statements which are involved in a function body. Therefore, it is possible to call
fuction whenever we want to process that functions statements.
Advantages :
It is easy to use.
Debugging is more suitable for programs.
It reduces the size of a program.
It is easy to understand the actual logic of a program.
Highly suited in case of large programs.
By using functions in a program, it is possible to construct modular and structured
programs.
It modularizes the software or program
Types of functions :
There are 2(two) types of functions as:
1. Built in Functions
2. User Defined Functions
1. Built in Functions :
These functions are also called as 'library functions'. These functions are provided by system.
These functions are stored in library files. e.g.
scanf()
printf()
strcpy
strlwr
strcmp
strlen
strcat
Syntax:
// Function prototype
<return_type><function_name>([<argu_list>]);
void main()
{
// Function Call
<function_name>([<arguments>]);
}
// Function definition
<return_type><function_name>([<argu_list>]);
1
{
<function_body>;
}
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c;
clrscr();
printf("\n Enter Any 2 Numbers : ");
scanf("%d %d",&a,&b);
c = a + b;
printf("\n Addition is : %d",c);
getch();
}
Program :
#include <stdio.h>
#include <conio.h>
void add();
void main()
{
add();
getch();
}
void add()
{
int a, b, c;
clrscr();
printf("\n Enter Any 2 Numbers : ");
scanf("%d %d",&a,&b);
c = a + b;
printf("\n Addition is : %d",c);
}
Output :
Enter Any 2 Numbers : 23 6
Addition is : 29_
2
Difference between user defined function and library function
The user-defined functions are defined by a user as per its own requirement and library
functions come with compiler.
Example:1
#include <stdio.h>
int main(void)
{
int number = 10;
int result = 0;
result = change(number);
printf("\nIn main, result = %d\tnumber = %d", result, number);
return 0;
}
3
Function calls function
#include<stdio.h>
f1 (void)
{
printf ("f1-1 \n");
f2 ( );
printf ("f1-2 \n");
}
f2 ()
{
printf ("f2 \n");
}
main ( )
{
printf ("1 \n");
f1 ( );
printf ("2 \n");
}
1
f1-1
f2
f1-2
2
Write a c program to define a function which adds two integer numbers and returns the result to
the main function
#include <stdio.h>
The value of k is 30
4
Recursion
Formal definitions of recursion
In mathematics and computer science, a class of objects or methods exhibit recursive behavior
when they can be defined by two properties:
1. A simple base case (or cases), and
2. A set of rules which reduce all other cases toward the base case.
For example, the following is a recursive definition of a person's ancestors:
One's parents are one's ancestors (base case).
The parents of one's ancestors are also one's ancestors (recursion step).
The Fibonacci sequence is a classic example of recursion:
Fib(0) is 0 [base case]
Fib(1) is 1 [base case]
For all integers n > 1: Fib(n) is (Fib(n-1) + Fib(n-2)) [recursive definition]
5
Characteristics of recursion function
1. A recursive function is a function that calls itself.
2. The speed of a recursive program is slower because of stack overheads.
3. In recursive function we need to specify recursive conditions, terminating conditions, and
recursive expressions
4. Recursion works on stack i.e, first in last out.
5. Recursion is a process of calling itself with different parameters until a base condition is
achieved. Stack overflow occurs when too many recursive calls are performed.
Recursion in a screen recording program, where the smaller window contains a snapshot of the
entire screen.
Write a recursive function to find sum of digits of any number input through keyboard.
main()
{
int s, n;
printf("\nEnter any number:");
scanf("%d",&n);
s =sum(n);
printf("\n Sum of digits = %d",s);
}
sum(int n)
{
if(n<10)
return(n); else
return(n %10 + sum(n/10)) ;
}
#include<stdio.h>
void printFibonacci(int);
int main(){
int k,n;
long int i=0,j=1,f;
printf("Enter the range of the Fibonacci series: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n);
return 0;
}
Sample output:
7
Enter the range of the Fibonacci series: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89
#include<stdio.h>
void printFibonacci(int);
int main(){
int k,n;
long int i=0,j=1,f;
return 0;
}
while(n>0){
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
n--;
}
}
#include
#include
void main(){
int i;
char string[5]="HELLO";
clrscr();
for(i=0;i<5;i++)
{
printf("\n %c",string[i]);
}
8
getch();
}
Here we have two indexes/subscripts. Normally the two indexes refer to the rows and
columns, that is the [6] refers to rows and [10] refers to columns. If we assign initial
string values for the 2D array it will look something like the following.
char Name[6][10] = {"Mr. Bean", "Mr. Bush", "Nicole", "Kidman", "Arnold", "Jodie"};
Here, we can initialize the array with 6 strings, each with maximum 9 characters long. If
depicted in rows and columns it will look something like the following and can be
considered as contiguous arrangement in the memory.