Unit4-Pps-functions &dynamic Memory Allocation
Unit4-Pps-functions &dynamic Memory Allocation
Tech I-Year
Syntax :
function returntype functionname( formal parameters)
2. function definition: the function definition is also called function prototype. within the function
definition we will write the code of task to be performed by the function.
Example :
functions #include<stdio.h>
#include<conio.h>
void add(int ,int); // function declaration
int subtraction(int ,int) // function declaration
void main()
{
int a,b,sub;
clrscr();
printf(“enter a b values
:”); scanf(“%d
%d”,&a,&b);
add(a,b); // function calling with actual
parameters sub=subtraction(a,b);
printf(“ %d-%d is :%d”,a,b,sub);
}
call by value: The values of the actual parameters of calling function are copied into the formal
parameters of the called function.
The called function may changed the values passed ,but the original value in the
calling function are unchanged.
call by reference(address):
passing the address to the called function instead of values is called call by reference .
The call by reference can be achieved by using pointers .
The address of the actual parameters of calling function are copied into the formal
parameters of the called function.
If any changes occurred for the values of the formal parameters in the called function
the changes will affect the actual parameters of the calling function because we are
passing the address .
Note : 1.use & (address symbol ) before the actual parameter when we call a function.
2.use *(asterisk ) symbol before the formal parameters in the called function
C provides a collection of standard functions whose definition(logic) have been written and are
ready to be used in our program.
The header file contains all the function declarations .The include statement used to include the
header file for our program.
Ex:include<stdio.h>
The library file stdio.h include the standard input output functions into our program.
It contains the declarations for printf()& scanf().when the program is linked the object code
for these functions is combined with our code to build the complete program.
strchar()- -to search a single character in the given string and find rest of data from match
strstr()----to search a string in the given string and return rest of data from match
strrev()----to reverse the given string
if(num == 0 || num == 1)
return(num);
else
//recursive call
return (fibonacci(num-1) + fibonacci(num-2));
}
#include<stdio.h>
int main(){
int n,n1=0,n2=1,n3;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series:\n ");
printf("%d\t %d \t",n1,n2);
n=n-2;
while(n >0)
{
n3=n1+n2;
n1=n2;
n2=n3;
printf("%d\t",n3);
n--;
}
return 0;
}
#include<stdio.h>
int main(){
int n,product=1,i;
printf("Enter the number : ");
scanf("%d",&n);
i=1;
while(i<=n)
{
product=product*i;
i++;
}
Array is a pointer variable when we pass the array as argument to the function it pass
the base address to the function
if any changes made in the called function the changes will affect the calling function
i.e passing arry to a function is a call by reference
return 0;
}
};
return 0;
}
Allocation of memory for the variable during run time is called dynamic
memory allocation.
There are three functions to allocate memory during runtime .
1. malloc()
2.calloc()
3.realloc()
C program read the array elements during runtime and find the sum of
array elements ( using malloc function)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int I,len,sum=0,*par;
printf(“enter the array size”);
scanf(“%d”,&len);
par=(int *) malloc(sizeof(int)*len);
for(i=0;i<len;i++)
{
printf(“\nenter number:”);
scanf(“%d”,&par[i]);
}
for(i=0;I<len,i++)
{
sum=sum+*(par+i);
}
printf(“the array elements are \n”);
for(i=0,i<n;i++)
{
printf(“\n par[%d]=%d”,I,*(par+i));
}
printf(“the sum of the array elements is :%d”,sum);
free(par);
}
the calloc function allocates 6 bytes for the array ptr of 3 elements
and array 3 elements are initialized with the value 0 .
ptr 0 0 0
ptr[0] ptr[1] ptr[2]
C program read the array elements during runtime and find the sum of
array elements ( using calloc function)
#include<stdio.h>
#include<stdlib.h>
Main()
{ int len,I;
Int *par
Printf( “enter the count of the numbers\n”);
Scanf(“%d”,&len);
syntax:
void * realloc (void *ptr , size )
Ptr: .the first argument ptr is a pointer to a block of memory for which the size is to be
altered.
Size: new size of the block in bytes
*array = 100;
*(array + 1) = 200;
}
note :
Int *B=(int *)realloc(NULL,n*sizeof(int) // equivalent to malloc
Int *B=(int*)realloc(A,0) // equivalent to free(A)