Unit 4 Functions and Pointers
Unit 4 Functions and Pointers
POINTERS
The functions defined by the user for their requirements are called user-
defined functions.
Whenever it is needed, the user can modify this function.
Example: sum(a,b)
Function declaration
Function definition
Function call
Syntax
Actual Parameter
These are the parameters which are transferred from the calling function to
the called function.
Formal Parameter
These are the parameters which are used in the called function.
The return statement may or may not send some values to the calling
function.
Syntax:
return; (or)
return (expression);
#include <stdio.h>
#include<conio.h>
void main()
{
int a,b;
void add(int,int);
printf("\nEnter two number:");
scanf("%d%d",&a,&b);
add(a,b);
}
void add(int x,int y) //function with arguments
{
int z;
z=x+y;
printf("\nSum is:%d",z);
PREETHA V - AP/CSE, SRIT.
}
Output
}
Output
There are two different ways of passing parameters to a method, they are:
Call by value
Call by reference
Syntax:
func1()
{
………..
func1();
…………
PREETHA V - AP/CSE, SRIT.
}
Example
#include<stdio.h> if(x==1)
#include<conio.h> return(1);
void main() else
{ f=x*rec(x-1);
int a; return(f);
}
int rec(int);
printf("\nEnter the number:");
scanf("%d",&a);
Output:
printf("The factorial of %d! is Enter the number:5
%d",a,rec(a));
The factorial of 5! is 120
}
int rec(int x)
{
PREETHA V - AP/CSE, SRIT.
int f;
Example: Working of 3!
abs(x):
It is used to find the absolute value of x
Example: abs(-36) is 36
pow(x,y):
It is used to find the value of xy
PREETHA V - AP/CSE, SRIT.
Example: pow(5,2) is 25
ceil(x):
It is used to find the smallest integer greater than or equal to
x.
Example: ceil(7.7) is 8
rand():
It is used to generate a random number.
sin(x):
It is used to find the sine value of x
Example: sin(30) is 0.5
cos (x):
It is used to find the cosine value of x
PREETHA V - AP/CSE, SRIT.
Ctype Functions
toascii(x):
It is used to find the ASCII value of x
Example: toascii(‘a’) is 97
toupper(x):
It is used to convert lowercase character to uppercase.
Example: toupper(‘a’) is A toupper(97) is A
tolower(x):
It is used to convert uppercase character to lowercase.
PREETHA V - AP/CSE, SRIT.
Example: tolower(‘A’) is a
Example:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<ctype.h>
void main()
{
int x,y=2;
printf("\nEnter the number:");
scanf("%d",&x);
printf("\nThe square root of %d is %f",x,sqrt(x));
printf("\nThe value of %d power%dis%f ",x,y,pow(6,2));
printf("\nThe ceiling of 6.7 is %f",ceil(6.7));
printf("\nThe floor of 6.7 is %f",floor(6.7));
PREETHA V - AP/CSE, SRIT.
printf("\nThe absolute value of -6 is %d",abs(-6));
printf("\nThe value of sin 45 is %f",sin(45));
printf("\nThe uppercase of 'a' is %c",toupper('a'));
printf("\nThe uppercase of 97 is %c",toupper(97));
getch();
}
Math functions
Computation of Sine Series
Random Number Generation
Tower of Hanoi
Factorial using Recursive functions.
Pointer is a variable that contains the address of another variable i.e.. direct
address of the memory location.
Like any variable or constant, you must declare a pointer before you can use
it to store any variable address.
x Variable
5 8714 Value
Address
Output
The Address of x = 8714
The Value of x = 5
PREETHA V - AP/CSE, SRIT.
Pointer Declaration
Syntax:
data-type *pointer-name;
Example: int *a;
Example:
int *a;
x=5;
a=&x;
#include<stdio.h>
#include<conio.h>
void main()
{
int y=10;
int *a;
a=&y;
printf("\n The Value of y = %d",y);
printf("\n The Address of y = %u",&y);
printf("\n The Value of a = %d",a);
printf("\n The Address of a = %u",&a);
}
PREETHA V - AP/CSE, SRIT.
Illustration of the example:
a y Variable
5001 10 Value
Address
8000 5001
The Value of y = 10
The Address of y = 5001
The Value of a = 5001
The Address of a = 8000
For Example:
int *a,*b;
a=b=0;
Example:
int x=10,*a,**b;
a=&x;
b=&a;
b a x Variable
8000 5001 10 Value
Address
9000 8000 5001
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
int *b,**c;
b=&a;
c=&b;
printf("\n The Value of a = %d",a);
printf("\n The Address of a = %u",&a);
printf("\n The Value of b = %d",b);
printf("\n The Address of b = %u",&b);
printf("\n The Value of c = %d",c);
printf("\n The Address of c = %u",&c);
}
PREETHA V - AP/CSE, SRIT.
Output
The Value of a = 10
The Address of a = 5001
The Value of b = 5001
The Address of b = 8000
The Value of c = 8000
The Address of c = 9000
Example
int a[3]={2,3,7};
int *b;
b=a;
b a[0] Variable
8744 2 Value
Address
9000 8744
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={2,3,7,9,10};
int i;
for(i=0;i<5;i++)
{
printf("\n The Value of a[%d] = %d",i,a[i]);
printf("\n The Address of a[%d] = %u",i,&a[i]);
}
}
PREETHA V - AP/CSE, SRIT.
Illustration of the example:
The Sum is 15
Syntax:
struct structure_name
{
structure element1;
structure element2;
…………………….
}variable,*ptr;
struct stud
{
int sno;
char name[10];
int mark;
};
memory can't be increased while executing memory can be increased while executing
program. program.
If memory is not sufficient for malloc() or calloc(), you can reallocate the
memory by realloc() function. In short, it changes the memory size.
Let's see the syntax of realloc() function.
ptr=realloc(ptr, new-size)
realloc() Example: