Computer Programming-Unit 4
Computer Programming-Unit 4
“Run like a horse until you WIN; Run faster than the horse after you WIN”
2. Explain predefined function and user-defined 3. Explain function prototypes with an example.
function with an example. Function with arguments, with return values
Pre-defined function:- Function with arguments, without return values
Pre-defined functions are functions that are Functions without arguments, with return values
created already and stored in the C – library. Functions without arguments, without return values
They perform certain operations.
Ex: printf(), scanf(), sqrt(), cos(), sin(), … Function with arguments, with return values:-
This type of function contains arguments and return values.
stdio.h printf() To print to the console output Return statement is used, arguments are used.
scanf() To get values from user Called function will return some value to the calling function
getch() To stay in console screen until Program:-
a key is pressed #include<stdio.h>
string.h strlen() To find length of a string #include<conio.h>
strrev() To reverse a string int add(int, int);
void main()
strcmp() To compare two strings {
math.h sin() To perform sine operation clrscr();
cos() To perform cosine operation int a = 5, b = 10;
sqrt() To perform square root operation printf(“%d”, add ( a, b) );
stdlib.h malloc() To allocate memory getch();
O/p:-
}
exit() To exit the execution
int add(int a, int b) 15
{
Example program:-
return a+b;
#include<stdio.h> }
O/p:-
#include<conio.h> Function with arguments, without return values:-
Mechanical This type of function contains arguments and no return values.
#include<string.h> 10
#include<math.h> Return statement is not used, arguments are used.
100 Called function will not return any value to the calling function
void main() 4 Program:-
{
#include<stdio.h>
clrscr(); #include<conio.h>
printf(“%d”, printf(“Mechanical”) ); void add(int, int);
printf(“%d”, sqrt ( 10 ) ); void main()
printf(“%d”, strlen(“mech”) ); {
getch(); clrscr();
exit(0); int a = 5, b = 10;
} add ( a, b);
User defined function:- getch();
O/p:-
They are defined by the user }
User can create their own function void add(int a, int b) 15
{
Function prototype should be mentioned before
Printf(“%d”,a+b);
main() function }
They perform certain operations. Function without arguments, with return values:-
#include<stdio.h> This type of function contains no arguments, with return values.
#include<conio.h> Return statement is used, arguments are not used
int add(int, int); Called function will return some value to the calling function
void main() Program:- int add()
{ {
#include<stdio.h>
clrscr(); return a+b;
#include<conio.h>
printf(“%d”, add(10, 20) ); }
int add();
getch(); O/p:- void main()
}
{ O/p:-
int add ( int a, int b) 30
clrscr();
{ 15
printf(“%d”,add() );
return a+b ;
getch();
}
}
“Run like a horse until you WIN; Run faster than the horse after you WIN”
Function without arguments, without return values:- 5._Write a C program to sort the array elements in
This type of function contains no arguments, no return values. ascending order
Return statement and argument are not used. #include<stdio.h>
Called function will not return any value to the calling function #include<conio.h>
Program:- void sort(int *, int);
#include<stdio.h> void main()
#include<conio.h> {
void add();
clrscr();
void main()
{ int n, a[10], i;
clrscr(); printf(“enter number of elements”);
add ( a, b) ; scanf(“%d”,&n);
getch(); printf(“enter elements”);
O/p:-
} for(i = 0; i < n; i++)
void add() 15 {
{ scanf(“%d”,&a[i]);
int a = 5, b = 10; }
printf(“%d”,a+b); sort(a,n);
} getch();
}
4._Explain recursion with an example.
void sort( int a[], int n)
(or)
{
Write a program to find the factorial of a number using
int i, j, temp;
recursion.
for(i = 0; i<n; i++) O/p:-
A function calls itself again and again
{ Enter number of elements
Until a condition is TRUE for( j = 0; j<n; j++)
5
“Run like a horse until you WIN; Run faster than the horse after you WIN”
Use of pointers:- Pointer addition:- Pointer subtraction:-
To make the program simple #include<stdio.h> #include<stdio.h>
To make the size of the program small. #include<conio.h> #include<conio.h>
To save memory space void main() void main()
{ {
To access array elements easily
clrscr(); clrscr();
To pass information between function int a = 5, b = 10; int a = 5, b = 10;
To increase the speed of execution of program int *x, *y; int *x, *y;
Program:- x = &a, y = &b; x = &a, y = &b;
printf(“%d”, (*x + *y)); printf(“%d”, (*x - *y));
#include<stdio.h>
getch(); getch();
#include<conio.h> } }
void main()
{ O/p:- O/p:- O/p:-
clrscr();
5 15 -5
int a;
int *b; 5 8. Explain call by value and call by reference.
a = 5;
b = &a; fff0 Call by value Call by reference
printf(“%d”,a); Values are passed in Address or pointers are
fff0
printf(“%d”,*b); function call passed in function call
printf(“%x”,&a);
If the values of the formal If the values of the formal
printf(“%x”,b);
parameters changed, parameters changed,
getch();
} values of the actual values of the actual
7._Explain pointer arithmetic in C. parameters will not parameters will also
(or) change change.
Explain the arithmetic operations using pointers in C. Slow process Fast process
There are four arithmetic operations that can be Different memory Same memory locations
done using pointers. They are:- locations used used
++ Pointer increment No chance or wrong Chances of wrong
-- Pointer decrement calculation calculation
+ Pointer addition Program:- Program:-
- Pointer subtraction #include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void swap(int, int); void swap(int, int);
Pointer increment:- Pointer decrement:-
void main() void main()
#include<stdio.h> #include<stdio.h>
{ {
#include<conio.h> #include<conio.h>
clrscr(); clrscr();
void main() void main()
int a=5, b=10; int a=5, b=10;
{ {
printf(“before %d%d”,a,b); printf(“before %d%d”,a,b);
clrscr(); clrscr(); swap(a,b); swap(&a,&b);
int a[3] = { 10, 20, 30 }; int a[3] = { 10, 20, 30 }; printf(“after %d%d”,a,b); printf(“after %d%d”,a,b);
int i, *ptr; int i, *ptr; getch(); getch();
ptr = a; ptr = a; } }
for(i = 0; i < 3; i++) for(i = 3; i > 0; i - -) void swap(int x, int y) void swap(int *x, int *y)
{ { { {
printf(“address=%x”,ptr); printf(“address=%x”,ptr);
int t; int t;
printf(value=%d”,*ptr); printf(value=%d”,*ptr); t = x; t = *x;
ptr++; ptr++; x = y; *x = *y;
} } y = t; *y = t;
getch(); getch(); printf(“inside %d%d”,x,y); printf(“inside %d%d”,*x, *y);
} } } }
O/p:- O/p:- O/p:- O/p:-
Address = fff0 value=10 Address = fff4 value=30 Before 5 10 Before 5 10
Address = fff2 value=20 Address = fff2 value=20 Inside 10 5 Inside 10 5
Address = fff4 value=30 Address = fff0 value=10 After 5 10 After 10 5
“Run like a horse until you WIN; Run faster than the horse after you WIN”