Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
105 views

Computer Programming-Unit 4

1. The document defines functions and pointers in C. It includes definitions of functions, pointers, and the differences between call by value and call by reference. 2. Function prototypes are defined as functions with arguments and return values, functions with arguments but no return values, functions without arguments but with return values, and functions without arguments or return values. 3. Predefined functions are functions in libraries that perform common operations. User-defined functions are created by the user to perform specific tasks.

Uploaded by

jaba123jaba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views

Computer Programming-Unit 4

1. The document defines functions and pointers in C. It includes definitions of functions, pointers, and the differences between call by value and call by reference. 2. Function prototypes are defined as functions with arguments and return values, functions with arguments but no return values, functions without arguments but with return values, and functions without arguments or return values. 3. Predefined functions are functions in libraries that perform common operations. User-defined functions are created by the user to perform specific tasks.

Uploaded by

jaba123jaba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

UNIT 4  Functions and Pointers

Part – A (2mark questions) 3. Define pointer in C.


 A pointer is a variable that is used to store the
1. Define Function in C address of another variable
 A function is a self-contained program, or a sub-  It is declared with *
program of one or more statements which is used  It is used to make the program small and save
to do some particular task. memory space
 Types of functions: i)User defined, ii)pre-defined.  Ex:-
2. What are pre-defined functions? Give ex. int a;
int *b;
 Pre-defined functions are functions that are
created already and stored in the C – library. b = &a;
 They perform certain operations. 4. What is the use of pointers in C?
Ex: printf(), scanf(), getch(), sqrt(), cos(), sin()..  To make the program simple
3. What is RETURN statement?  To make the size of the program small.
 It is used to return the control from calling function  To save memory space
to the next statement in the program.  To access array elements easily
 It can also return some values.  To pass information between function
 Ex:-  To increase the speed of execution of program
return; return 0; return(a+b); 5. Write the differences betweencall by value
4. What are the types of functions based on and call by reference.
return values?
(or) Call by value Call by reference
What are the types of function prototypes?
Values are passed in Address or pointers are
 Function with arguments, with return values function call passed in function call
 Function with arguments, without return values
 Functions without arguments, with return values If the values of the formal If the values of the formal
 Functions without arguments, without return values parameters changed, parameters changed,
5. Define recursion. values of the actual values of the actual
 A function calls itself again and again parameters will not parameters will also
 Until a condition is TRUE change change.
 This process is called as Recursion. Part – B (8 and 16 mark questions)
Ex:-
function() 1. Write a C program to print the sum of digits of a
{ number using recursion.
function();
} #include<stdio.h>
#include<conio.h>
6. What are the advantages and disadvantages of void main()
recursion? {
clrscr();
Advantages Disadvantages int n, r, sum;
printf(“enter a number”);
Less number of Difficult to clear errors
scanf(“%d”,&n);
statements
while(n>0) O/p:-
It needs less number of Difficult to think the logic of { Enter a number
variables a function r = n % 10; 234
sum = sum + r;
It is useful in branching Not easy to write code n = n /10;
process }
printf(“answer=%d”,sum);
getch();
}

“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

 This process is called as Recursion. { Enter elements


Ex:- if ( a[j] > a[j+1])
6 2 3 8 9
function() { Answer = 2 3 6 8 9
{ temp = a[j];
function(); a[j] = a[j+1];
} a[j+1]=temp;
}
Advantages Disadvantages }
}
Less number of Difficult to clear errors printf(“answer = “);
statements for( i = 0; i < n; i++)
{
It needs less number of Difficult to think the logic of printf(“%d”, a[i]);
variables a function }
}
It is useful in branching Not easy to write code
process
6. Write a C program to explain pointers.
 A pointer is a variable that is used to store the
Program:- int factorial(int n)
address of another variable
#include<stdio.h> {
#include<conio.h> if ( n = = 1)  It is declared with *
int factorial ( int);
return 1;  It is used to make the program small and save
else memory space
void main() return (n*factorial(n-1) );
{ }  Ex:-
clrscr(); int a;
int n, f; int *b;
printf(“enter a number”); O/p:- b = &a;
scanf(“%d”,&n); Enter a number 5
printf(“answer =”); Answer = 120
printf(“%d”,factorial (n) );
getch();
}

“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

Address = fff0 value=10 Address = fff0 value=10

“Run like a horse until you WIN; Run faster than the horse after you WIN”

You might also like