You are on page 1of 9

Govt. College of Engineering.

& Ceramic Technology


Class Notes
Pranay Adak connect2pranay.2012@gmail.com

Functions and Function Pointers

1. Declaring(Prototype), Defining and Calling a function by passing the


value of the parameters (Call by Value).

#include <stdio.h>
#include <stdlib.h>
int add(int,int); Function Prototype
int main(void) a 2
{
int a,b,r; b 3
printf("Enter a and b:");
scanf("%d%d",&a,&b); r 5
r=add(a,b); Function Calling
printf("Result=%d",r); x 2
return 0;
y 3
}
int add(int x,int y)
{ Function Definition
return x+y;
}

2. Declaring(Prototype), Defining and Calling a function by passing the


address of the parameters (Call by Value).
#include <stdio.h>
#include <stdlib.h>
int add(int *,int *); int a 5 &a
int main(void)
{ int b 6 &b
int a,b,r;
printf("Enter a and b:"); int r 11 &r
scanf("%d%d",&a,&b);
r=add(&a,&b); int * x &a &x
printf("Result=%d",r); int * y &b &y
return 0;
}
int add(int *x,int *y)
{
return ((*x)+(*y));
}

1
Govt. College of Engineering. & Ceramic Technology
Class Notes
Pranay Adak connect2pranay.2012@gmail.com

3. Declaring(Prototype), Defining and Calling a function another


example (Call by Value).
#include <stdio.h>
#include <stdlib.h>
int factorial(int);
int main(void)
{
int n,f;
printf("Enter the number:");
scanf("%d",&n);
f=factorial(n);
printf("Factorial of %d is =%d",n,f);
return 0;
}
int factorial(int n)
{
int i,fact=1;
for(i=1;i<=n;i++)
{
fact*=i;
}
return fact;
}

4. Example of function returning pointer.


#include <stdio.h>
#include <stdlib.h>
int *factorial(int);
int main(void)
{
int n;
int *f;
printf("Enter the number:");
scanf("%d",&n);
f=factorial(n);
printf("Factorial of %d is =%d",n,*f);
return 0;
}
int *factorial(int n)
{
int i;
int *fact;
fact=(int *)malloc(sizeof(int));
*fact=1;
for(i=1;i<=n;i++)
{

2
Govt. College of Engineering. & Ceramic Technology
Class Notes
Pranay Adak connect2pranay.2012@gmail.com

(*fact)=(*fact)*i;
}
return fact;
}

5. Example of passing a 2D array to a function as parameter and


returning a 2D array from a function.

#include <stdio.h>
#include <stdlib.h>
int **create2dArray(int,int);
void init2dArray(int **,int,int);
void display2dArray(int **,int,int);
int main(void)
{
int row;
int col;
int **a;
printf("Enter the no of rows and columns respectively:");
scanf("%d%d",&row,&col);
a=create2dArray(row,col);
init2dArray(a,row,col);
display2dArray(a,row,col);
return 0;
}
int **create2dArray(int r,int c)
{
int i;
int **p;
p=(int **)calloc(r,sizeof(int *));
if(p!=NULL)
{
for(i=0;i<r;i++)
{
*(p+i)=(int *)calloc(c,sizeof(int));
}
}
else
{
printf("Error!!!\n");
exit(1);
}
return p;
}
void init2dArray(int **p,int r,int c)

3
Govt. College of Engineering. & Ceramic Technology
Class Notes
Pranay Adak connect2pranay.2012@gmail.com

{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter a[%d][%d]=",i,j);
scanf("%d",*(p+i)+j);
}
}
}
void display2dArray(int **p,int r,int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d ",*(*(p+i)+j));
}
printf("\n");
}
}

Function pointers
Declaring a function pointer, -

<FunctionReturnType> (*<FunctionPointerName/id>)(<arguments>);

Example:
For the function prototype “int add(int,int);” the
declaration is,- int (*fnp)(int,int);

Assigning a value into a function pointer, -

<FunctionPointerName/id>=&<FunctionName>;

4
Govt. College of Engineering. & Ceramic Technology
Class Notes
Pranay Adak connect2pranay.2012@gmail.com

Example:
fnp=&add;

Calling a function using function pointer, -

(*<FunctionPointerName/id>)(<arguments>);

Example:
r=(*fnp)(a,b);

6. Calling function using function pointer (The same Example 1 using


function pointer).
#include <stdio.h>
#include <stdlib.h>
int add(int,int);
int main(void)
{
int a,b,r;
int (*fnp)(int,int);
printf("Enter a and b:");
scanf("%d%d",&a,&b);
fnp=&add;
r=(*fnp)(a,b);
printf("Result=%d",r);
return 0;
}
int add(int x,int y)
{
return x+y;
}

7. Another example of Calling function using function pointer (The


same Example 4 using function pointer).

#include <stdio.h>
#include <stdlib.h>
int *factorial(int);
int main(void)
{
int n;
int *f;
int *(*fnp)(int);
printf("Enter the number:");
scanf("%d",&n);
fnp=&factorial;
f=(*fnp)(n);
printf("Factorial of %d is =%d",n,*f);

5
Govt. College of Engineering. & Ceramic Technology
Class Notes
Pranay Adak connect2pranay.2012@gmail.com

return 0;
}
int *factorial(int n)
{
int i;
int *fact;
fact=(int *)malloc(sizeof(int));
*fact=1;
for(i=1;i<=n;i++)
{
(*fact)=(*fact)*i;
}
return fact;
}

8. Create a dynamic 1D array using three different functions to create


initialize and display array. Call the functions using function
pointer.
#include <stdio.h>
#include <stdlib.h>
int *create(int);
void init(int *,int);
void display(int *,int);
int main(void)
{
int n;
int *a;
int *(*fnp_create)(int);
int *(*fnp_init)(int *,int);
int *(*fnp_display)(int *,int);
printf("Enter n:");
scanf("%d",&n);
fnp_create=&create;
fnp_init=&init;
fnp_display=&display;
a=(*fnp_create)(n);
(*fnp_init)(a,n);
(*fnp_display)(a,n);
return 0;
}
int *create(int x)
{
int i;

6
Govt. College of Engineering. & Ceramic Technology
Class Notes
Pranay Adak connect2pranay.2012@gmail.com

int *p;
p=(int *)malloc(x*sizeof(int));
if(p==NULL)
{
printf("Error!!!");
}
return p;
}
void init(int *p,int x)
{
int i;
for(i=0;i<x;i++)
{
printf("Enter a[%d]=",i);
scanf("%d",p+i);
}
}
void display(int *p,int x)
{
int i;
for(i=0;i<x;i++)
{
printf("%d ",*(p+i));
}
}

7
Govt. College of Engineering. & Ceramic Technology
Class Notes
Pranay Adak connect2pranay.2012@gmail.com

Array of Function Pointers

Declaring an array of function pointers, -

<FunctionReturnType> (*<FunctionPointerName/id[size]>)(<arguments>);

Example:
For the function prototypes “int add(int,int); and int sub(int,int);”
the declaration and assigning the values is,-
int (*fnp[])(int,int)={&add,&sub};
Calling a function using function pointer array, -

(*<FunctionPointerName/id>[i])(<arguments>);

Example:
r=(*fnp[i])(a,b);

9. Create a dynamic 1D array using three different functions to create


initialize and display array. Call the functions similar in prototypes
using array of function pointers.
#include <stdio.h>
#include <stdlib.h>
int *create(int);
void init(int *,int);
void display(int *,int);
int main(void)
{
int n,i;
int *a;
int *(*fnp_create)(int);
void (*fnp[])(int *,int)={&init,&display};
printf("Enter n:");
scanf("%d",&n);
fnp_create=&create;
a=(*fnp_create)(n);
for(i=0;i<2;i++)
{
(*(*(fnp+i)))(a,n); //or (*fnp[i])(a,n);
}
return 0;
}
int *create(int x)
{
int *p;

8
Govt. College of Engineering. & Ceramic Technology
Class Notes
Pranay Adak connect2pranay.2012@gmail.com

p=(int *)malloc(x*sizeof(int));
if(p==NULL)
{
printf("Error!!!");
}
return p;
}
void init(int *p,int x)
{
int i;
for(i=0;i<x;i++)
{
printf("Enter a[%d]=",i);
scanf("%d",p+i);
}
}
void display(int *p,int x)
{
int i;
for(i=0;i<x;i++)
{
printf("%d ",*(p+i));
}
}

You might also like