FUNCTIONS
Function is a
collection of statements or programs. More than one time we have to call the
function through the main function.
(call by value,
call by reference)
Types
Built-in-functions or Pre-defined
functions
User-defined functions
User-defined
functions
No argument No
return type
With argument No
return type
With argument With
return type
No
argument No return type
#include<stdio.h>
#include<conio.h>
void arunya();
main()
{
clrscr();
arunya();
arunya();
arunya();
}
void arunya()
{
printf("Hai
Students \n");
getch();
}
With
argument No return type
#include<stdio.h>
#include<conio.h>
void add(int,int);
main()
{
int a,b;
clrscr();
printf("Enter
the values=");
scanf("%d%d",&a,&b);
add(a,b);
}
void add(int a,int
b)
{
int c;
c=a+b;
printf("Result
is=%d",c);
}
With
argument With return type
#include<stdio.h>
#include<conio.h>
int add(int,int);
main()
{
int a,b,c;
clrscr();
printf("Enter
the values=");
scanf("%d%d",&a,&b);
c=add(a,b);
printf("Result=%d",c);
}
int add(int a,int
b)
{
int g;
g=a+b;
return g;
}
POINTERS
Pointer is a
variable which holds the address of another variable. By using this pointer
variable we may call that particular memory location.
Pointer is a
special variable, which contains the address of the memory location of another
variable.
A pointer is an
address of an object in memory. Because an array name is itself a pointer, the
uses of arrays and pointers are intimately related.
Usage
of pointers
A pointer used to
access a variable outside the function
Pointers reduce
the length of the program
It increases the
execution speed
Occupy less space
Example
#include<stdio.h>
#include<conio.h>
main()
{
int a=10,*p;
clrscr();
p=&a;
printf("The
value of a=%d \n",a);
printf("The
value of &a=%d \n",&a);
printf("The
value of p=%d \n",p);
printf("The
value of *p=%d \n",*p);
getch();
}
Declaration
<Data
type>*<variable>
Int a-10;
Int *p;
P=&a;
Char c=’s’,*cp;
Cp=&c;
Example
1
#include<stdio.h>
#include<conio.h>
main()
{
int a=10;
clrscr();
printf("\n\n
%d",&a);
getch();
}
Example
2
#include<stdio.h>
#include<conio.h>
main()
{
int *p;
int a;
clrscr();
printf("Enter");
scanf("%d",&a);
p=&a;
printf("\nA
%d",a);
printf("\nAdd
%d",&a);
printf("\nA
%d",*p);
printf("\nAdd
%d",p);
getch();
}
Add
two numbers
#include<stdio.h>
#include<conio.h>
main()
{
int *p,*q;
int a,b,s;
clrscr();
printf("Enter
two values:");
scanf("%d%d",&a,&b);
p=&a;
q=&b;
s=*p+*q;
printf("\nA
%d",s);
getch();
}
Pointers
and Arrays
Biggest and Smallest
#include<stdio.h>
#include<conio.h>
main()
{
int i,n,a[10],b,s;
clrscr();
printf("Enter
the Value:");
scanf("%d",&n);
printf("Enter
the Values:");
for(i=0;i<n;i++)
scanf("%d",(a+i));
b=*(a+0);
for(i=0;i<n;i++)
{
if(*(a+i)>b)
b=*(a+i);
else
s=*(a+i);
}
printf("Biggest
Value:%d",b);
printf("\nSmallest
Value:%d",s);
getch();
}
Single
dimension
#include<stdio.h>
#include<conio.h>
main()
{
int *a[3],n,i;
clrscr();
printf("Enter
the value:");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",(a+i));
for(i=0;i<n;i++)
printf("\n
%d",*(a+i));
getch();
}
Pointers with
function
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,*p,*q;
void swap(int
*,int *);
clrscr();
printf("Enter");
scanf("%d%d",&a,&b);
printf("\n
before interchange");
printf("\n
A=%d B=%d",a,b);
p=&a;
q=&b;
swap(p,q);
printf("\nAfter
interchange");
printf("\n
A=%d B=%d",a,b);
getch();
}
void swap(int
*m,int *n)
{
int *t;
*t=*m;
*m=*n;
*n=*t;
}
Structure
with pointers
#include<stdio.h>
#include<conio.h>
struct student
{
int
rno,age,m1,m2,m3;
char name;
};
main()
{
struct student
*stud;
int tot;
float avg;
clrscr();
printf("Enter
name");
scanf("%s",&stud->name);
printf("Enter
age");
scanf("%d",&stud->age);
printf("Enter
rno");
scanf("%d",&stud->rno);
printf("Enter
marks");
scanf("%d%d%d",&stud->m1,&stud->m2,&stud->m3);
tot=stud->m1+stud->m2+stud->m3;
avg=tot/3.0;
printf("\n
total marks %d",tot);
printf("\n
average %f",avg);
getch();
}

No comments:
Post a Comment