Professional Documents
Culture Documents
Unit-4: Functions
Unit-4: Functions
Functions
Function:
A function is a self block of statements that performs a particular task.
It is reusable
Why use function:
1) functions avoids rewriting the same code over and over .
2) functions can be called any number of times in a program and from any place in a
program.
3) We can track a large C program easily when it is divided into multiple functions.
4) Reusability is the main achievement of C functions.
5) It reduces the complexity of a big program and optimizes the code.
Types of Functions
a. Function Declaration
b. Function Call
c. Function Definition
a. Function Declaration
A function declaration provides the information regarding the function being used
in a program to the compiler.
It is also known as function prototype.
Syn:
b. Function Call
In order to use the function we need to invoke it at a required place in the
program. This is known as the function call.
Syn:
function_name (val1,val2);
c. Function Definition
A function defination is a self block of statements that performs a particular
task.
Syn:
return_type function_name (type, type,…)
{
------------------------
-----------------------
}
A function depending on whether arguments are present or not and whether a value is
returned or
not may belong to any one of the following categories:
(i ) Functions with no arguments and no return values.
(ii) Functions with arguments and no return values.
(iii) Functions with arguments and return values.
(iv) Functions with no arguments and return values
Program:
#include<stdio.h>
void add();
void main( )
{
add();
}
void add()
{
int a,b,res;
a=5;
b=6;
res=a+b;
printf("the addition is %d",res);
}
Output:
the addition is 11
(ii) Functions with arguments and no return values:-
The called function does receive data from calling function and does not return any values
to calling function.
Ex: void add(int,int );
Program:
#include<stdio.h>
void add(int,int);
void main( )
{
int a,b;
a=5;
b=6;
add(a,b);
}
void add(int a,int b)
{
int res;
res=a+b;
printf("the addition is %d",res);
}
Output:
the addition is 11
Program:
#include<stdio.h>
int add();
void main( )
{
printf("the addition is %d",add());
}
int add()
{
int a,b,res;
a=5;
b=6;
res=a+b;
return res;
}
Output:
the addition is 11
#include<stdio.h>
int add(int,int);
void main( )
{
int a,b;
a=5;
b=6;
printf("the addition is %d",add(a,b));
}
int add(int a,int b)
{
int res;
res=a+b;
return res;
}
Output:
the addition is 11
Actual parameters: The parameters of function call are known as formal parameters
Formal Parameters: The parameters of function definition are known as formal parameters.
Parameter Passing Mechanisms in C-Language:
Most programming languages have 2 strategies to pass parameters. They are
(i) pass by value( Call by Value)
(ii) pass by reference (Call by Reference)
Program:
In the above program we modified formal arguments ‘x’ by adding 5 and ‘y’ by adding 10
but those values are not reflected in actual arguments.
Here we copied the values of ‘a’ and ‘b’ into ‘x’ and ‘y’ variables.
(ii) Pass by reference (or) call by address :-
In this method calling function sends address of actual values as a parameter to called
function, called function performs its task and sends the result back to calling function. Thus,
the changes in called function reflect the original values of calling function. To return
multiple values from called to calling function we use pointer variables.
Calling function needs to pass “&‟ operator along with actual arguments and called
function need to use “*‟ operator along with formal arguments.
Changing data through an address variable is known as indirect access and “*‟ is
represented as indirection operator.
Program:
In the above program we modified formal arguments ‘*x’ by adding 5 and ‘*y’ by adding 10
and those values are reflected in actual arguments also.
Here we copied the address of ‘a’ and ‘b’ into ‘*x’ and ‘*y’ variables.
Program:
#include<stdio.h>
void display(int a[][2]);
void main()
{
int a[2][2]={1,2,3,4};
display(a);
}
void display(int x[][2])
{
int i,j;
printf("the array elements are:\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
}
Output:
the array elements are:
1 2
3 4
Recursion
Recursion is a process by which a function calls itself repeatedly, until some specified condition has
been satisfied.
Types Of Recursion
1) Direct Recursion
2) Indirect Recursion
1) Direct Recursion
A function is said to be directly recursive if it explicitly calls itself.
Structure Example
int m( )
{
return m();
}
2) Indirect Recursion
A function is said to be indirectly recursive if it contains a call to another function which
ultimately calls it.
Structure Example
int m( )
{
return n( );
}
int n( )
{
return m( )
}
Programs:
1. Write a c program to find factorial of a given number using recursion.
Program:
#include<stdio.h>
int Recfact(int);
void main( )
{
int n, f ;
printf("enter any number");
scanf("%d",&n);
f=Recfact(n);
printf("Factorial of %d using recursive function is %d",n,f);
}
int Recfact(int n)
{
if(n==0)
{
return 1;
}
else
{
return(n*Recfact(n -1));
}
}
Output:
enter any number
5
Factorial of 5 using recursive function is 120
2. Write a c program to find Fibonacci series upto a given number using recursion.
Program:
#include <stdio.h>
int fibonacciSeries(i){
if (i <= 1)
return i;
else
return (fibonacciSeries(i - 1) + fibonacciSeries(i - 2));
}
int main(void)
{
int num, i;
printf(“Enter a number to print Fibonacci series upto a given number”);
scanf(“%d”,&num);
printf(“The Fibonacci series is:”);
Output:
Enter a number to print Fibonacci series upto a given number
7
The Fibonacci series is: 0 1 1 2 3 5 8
Program:
#include<stdio.h>
int main(void)
int a,b;
int temp,rem;
if(div>divd)
temp=divd;
divd=div;
div=temp;
rem=divd%div;
while(rem>0)
divd=div;
div=rem;
rem=divd%div;
return div;
}
Output:-
Program:
#include<stdio.h>
int Recpower(int,int);
void main( )
{
int x,n,res;
scanf("%d",&x);
scanf("%d",&n);
res=Recpower(x,n);
printf("%d ^ %d = %d",x,n,res);
if(n == 0)
return 1;
else
return ( x * Recpower(x,n-1));
Output:-
2^ 4 =16
DMA
Static memory allocation vs Dynamic memory allocation
Static memory allocation Dynamic memory allocation
It is the process of allocating memory at It is the process of allocating memory
compile time. during execution of program.
Fixed number of bytes will be allocated. Memory is allocated as and when it is
needed.
The memory is allocated in stack memory The memory is allocated in heap
memory.
Execution is faster Execution is slow
Ex: Arrays Ex: Dynamic arrays, Linked List, Trees
syntax : free(ptr)
Program:
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,*ptr;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*) malloc(n*sizeof(int));
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
}
printf(" The list of elemnents are");
for(i=0;i<n;++i)
{
printf("%d",*(ptr+i));
}
free(ptr);
}
#include<stdio.h>
#include<stdlib.h>
void main(){
int n,i,*ptr;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*) calloc(n,sizeof(int));
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
}
printf(" The list of elemnents are");
for(i=0;i<n;++i)
{
printf("%d",*(ptr+i));
}
free(ptr);
}
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int*) malloc(3 * sizeof(int));
ptr[0] = 1;
ptr[1] = 2;
ptr[2] = 3;
Uses of DMA:
• Memory can be increased while executing program.
• used in implementing linked list
• Memory can be used more efficiently.