UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more...
-
Upload
abner-austin-horton -
Category
Documents
-
view
226 -
download
0
Transcript of UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more...
![Page 1: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/1.jpg)
UNIT - 4
FUNCTIONS AND POINTERS
![Page 2: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/2.jpg)
FUNCTION
• Functions is a sub-program that contains one or more statements and it performs some task when called.
![Page 3: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/3.jpg)
Types
Functions
User-DefinedFunctions
Pre-DefinedFunctions
![Page 4: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/4.jpg)
Pre-Defined Functions
• The pre-defined functions or library functions are built-in functions.
• The user can use the functions, but cannot modify the function.
• Example: sqrt()
![Page 5: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/5.jpg)
User-Defined Functions
• The functions defined by the user for their requirement are called user-defined functions.
• Whenever it is needed, The user can modify the function.
• Example: sum(a,b)
![Page 6: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/6.jpg)
Advantage of User-Defined Functions
• The length of the source program can be reduced.
• It is easy to locate error.• It avoid coding of repeated instructions.
![Page 7: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/7.jpg)
Elements of User-Defined Function
• Function declaration• Function definition• Function call
![Page 8: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/8.jpg)
Function• Syntax
datatype function_name (parameters list) {local variable declaration;…………………………body of the function;…………………………return(expression);}
![Page 9: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/9.jpg)
How Function Works
• Once a function is called the control passes to the called function.
• The working of calling function is temporarily stopped.
• When the execution of called function is completed then the control return back to the calling function and execute the next statement.
![Page 10: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/10.jpg)
![Page 11: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/11.jpg)
Parameters
• Actual ParameterThese are the parameters transferred
from the calling function to the called function.
• Formal ParameterThese are the parameters which is used
in the called function.
![Page 12: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/12.jpg)
![Page 13: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/13.jpg)
return Statement
• The return statement may or may not send some values to the calling function.
• Syntax:return; (or)return(expression);
![Page 14: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/14.jpg)
Function Prototypes
• Function with no arguments and no return values.
• Function with arguments and no return values.
• Function with arguments and return values.• Function with no arguments and with return
values.
![Page 15: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/15.jpg)
Function with no argumentsand no return values
• Here no data transfer take place between the calling function and the called function.
• These functions act independently, i.e. they get input and display output in the same block.
![Page 16: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/16.jpg)
![Page 17: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/17.jpg)
Example#include <stdio.h>#include<conio.h>void main() //calling function{
void add(void);add();
}void add() //called function{ int a,b,c; printf("\nEnter two number:"); scanf("%d%d",&a,&b); c=a+b; printf("\nSum is:%d",c);}
![Page 18: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/18.jpg)
Output
Enter two number:34
Sum is:7
![Page 19: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/19.jpg)
Function with argumentsand no return values
• Here data transfer take place between the calling function and the called function.
• It is a one way data communication, i.e. the called program receives data from calling program but it does not return any value to the calling program.
![Page 20: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/20.jpg)
![Page 21: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/21.jpg)
Example#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);}
![Page 22: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/22.jpg)
Output
Enter two number:24
Sum is:6
![Page 23: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/23.jpg)
Example#include <stdio.h>#include<conio.h>void main(){
int a,b;void add(int a,int b);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);}
![Page 24: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/24.jpg)
Output
Enter two number:24
Sum is:6
![Page 25: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/25.jpg)
Function with argumentsand return values
• Here data transfer take place between the calling function and the called function as well as between called function and calling function .
• It is a two way data communication, i.e. the called program receives data from calling program and it return some value to the calling program.
![Page 26: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/26.jpg)
![Page 27: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/27.jpg)
Example#include <stdio.h>#include<conio.h>void main(){
int a,b,c;int add(int,int);printf("\nEnter two number:");scanf("%d%d",&a,&b);c=add(a,b);printf("\nSum is:%d",c);
}int add(int x,int y){ int z; z=x+y; return(z);}
![Page 28: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/28.jpg)
Output
Enter two number:67
Sum is:13
![Page 29: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/29.jpg)
Function with no argumentsand with return values
• Here data transfer take place between the called function and the calling function.
• It is a one way data communication, i.e. the called program does not receives data from calling program but it return some value to the calling program.
![Page 30: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/30.jpg)
![Page 31: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/31.jpg)
#include <stdio.h>
#include<conio.h>
void main()
{
int add(),d;
d=add();
printf("\nSum is:%d",d);
}
int add() //function wit no argument
{ int a,b,c;
printf("\nEnter two number:");
scanf("%d%d",&a,&b);
c=a+b;
return(c);
}
![Page 32: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/32.jpg)
Output
Enter two number:5
8
Sum is:13
![Page 33: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/33.jpg)
Parameter Passing Methods
• Call by value• Call by reference
![Page 34: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/34.jpg)
Call by value
• Actual argument passed to the formal argument.
• Any changes to the formal argument does not affect the actual argument.
![Page 35: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/35.jpg)
Example#include <stdio.h>#include<conio.h>void main(){
int x,y,change(int,int);printf("\nEnter value of x:");scanf("%d",&x);printf("\nEnter value of y:");scanf("%d",&y);
![Page 36: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/36.jpg)
change(x,y);printf("\n\nValues in the Main()-->x=%d,y=%d",x,y);
}int change(int a,int b){ int c; c=a; a=b; b=c; printf("\nValues in the Fuction -->x=%d,y=%d",a,b);}
![Page 37: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/37.jpg)
Output
Enter value of x:5
Enter value of y:6
Values in the Fuction -->x=6,y=5
Values in the Main()-->x=5,y=6
![Page 38: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/38.jpg)
Call by reference
• Instead of passing value, the address of the argument will be passed.
• Any changes to the formal argument will affect the actual argument.
![Page 39: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/39.jpg)
Example#include <stdio.h>#include<conio.h>void main(){
int x,y,change(int*,int*);printf("\nEnter value of x:");scanf("%d",&x);printf("\nEnter value of y:");scanf("%d",&y);
![Page 40: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/40.jpg)
change(&x,&y);printf("\n\nValues in the Main()-->x=%d,y=%d",x,y);
}int change(int *a,int *b){ int c; c=*a;
*a=*b; *b=c; printf("\nValues in the Function -->x=%d,y=%d",*a,*b);}
![Page 41: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/41.jpg)
Output
Enter value of x:5
Enter value of y:6
Values in the Function -->x=6,y=5
Values in the Main()-->x=6,y=5
![Page 42: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/42.jpg)
Recursion• It is a process of calling the same function
itself again and again until some condition is satisfied.
• Syntax:
func1()
{
………..
func1();
}
![Page 43: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/43.jpg)
Example#include<stdio.h>#include<conio.h>void main(){ int a; int rec(int); printf("\nEnter the number:"); scanf("%d",&a); printf("The factorial of %d! is %d",a,rec(a));}
![Page 44: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/44.jpg)
int rec(int x){ int f; if(x==1)
return(1); else
f=x*rec(x-1); return(f);}
Output:Enter the number:5The factorial of 5! is 120
![Page 45: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/45.jpg)
Example: Working of 3!
![Page 46: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/46.jpg)
Tower of Honoi
123
323
1323
![Page 47: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/47.jpg)
Tower of Honoi
123
323
1323
![Page 48: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/48.jpg)
Library Function
• It is pre-defined function.• The library function provides functions like
mathematical, string manipulation etc,.
![Page 49: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/49.jpg)
Examplesqrt(x):
It is used to find the square root of xExample: sqrt(36) is 6abs(x):
It is used to find the absolute value of xExample: abs(-36) is 36pow(x,y):
It is used to find the value of xy
Example: pow(5,2) is 25ceil(x):
It is used to find the smallest integer greater than or equal to x
Example: ceil(7.7) is 8
![Page 50: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/50.jpg)
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.5cos(x):
It is used to find the cosine value of xExample: cos(30) is 0.86tan(x):
It is used to find the tan value of xExample: tan(30) is 0.577
![Page 51: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/51.jpg)
toascii(x):It is used to find the ASCII value of x
Example: toascii(a) is 97toupper(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.Example: tolower(‘A’) is a
![Page 52: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/52.jpg)
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 squareroot of %d is %f",x,sqrt(x));
printf("\nThe value of %d power%dis%f ",x,y,pow(6,2));
![Page 53: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/53.jpg)
printf("\nThe ceiling of 6.7 is %f",ceil(6.7));
printf("\nThe floor of 6.7 is %f",floor(6.7));
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();
}
![Page 54: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/54.jpg)
Output:Enter the number:6
The squareroot of 6 is 2.449490The value of 6 power 2 is 36.000000The ceiling of 6.7 is 7.000000The floor of 6.7 is 6.000000The absolute value of -6 is 6The value of sin 45 is 0.850904The uppercase of 'a' is AThe uppercase of 97 is A
![Page 55: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/55.jpg)
Pointers
• Pointer is a variable that contains the memory address of another variable.
![Page 56: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/56.jpg)
Example:
x=5
x Variable
1002 Address
5 Value
![Page 57: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/57.jpg)
Example#include<stdio.h>#include<conio.h>void main(){ int x=5; printf("\n The Address of x = %u",&x); printf("\n The Value of x = %d",x);}
OutputThe Address of x = 8714The Value of x = 5
![Page 58: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/58.jpg)
Pointer Declaration• Syntax
data-type *pointer-name;
data-type - Type of the data to which the pointer points.
pointer-name - Name of the pointer
• Example: int *a;
![Page 59: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/59.jpg)
Accessing Variable through Pointer
• If a pointer is declared and assigned to a variable, then the variable can be accessed through the pointer.
• Example:int *a;x=5;a=&x;
![Page 60: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/60.jpg)
• Example
#include<stdio.h>#include<conio.h>void main(){ int x=5; int *a; a=&x; printf("\n The Value of x = %d",x); printf("\n The Address of x = %u",&x); printf("\n The Value of a = %d",a); printf("\n The Value of x = %d",*a);}
![Page 61: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/61.jpg)
Output
The Value of x = 5 The Address of x = 8758 The Value of a = 8758 The Value of x = 5
![Page 62: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/62.jpg)
Example:#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);}
![Page 63: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/63.jpg)
5001 10
8000
a y
5001
Variable
Value
Address
![Page 64: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/64.jpg)
Output
The Value of y = 10The Address of y = 5001The Value of a = 5001The Address of a = 8000
![Page 65: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/65.jpg)
Null Pointer
• A pointer is said to be null pointer if zero is assigned to the pointer.
• Exampleint *a,*b;a=b=0;
![Page 66: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/66.jpg)
Pointer to Pointer
• Here one pointer stores the address of another pointer variable.
• Example:int x=10,*a,**b;a=&x;b=&a;
![Page 67: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/67.jpg)
5001 10
8000
a x
5001
Variable
Value
Address
8000
9000
b
![Page 68: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/68.jpg)
Example#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);}
![Page 69: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/69.jpg)
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
![Page 70: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/70.jpg)
Pointers and Functions
• Call by Value• Call by Reference
![Page 71: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/71.jpg)
Call by value
• Actual argument passed to the formal argument.
• Any changes to the formal argument does not affect the actual argument.
![Page 72: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/72.jpg)
Example#include <stdio.h>#include<conio.h>void main(){
int x,y,swap(int,int);printf("\nEnter value of x:");scanf("%d",&x);printf("\nEnter value of y:");scanf("%d",&y);
![Page 73: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/73.jpg)
change(x,y);printf("\n\nValues in the Main()-->x=%d,y=%d",x,y);
}int swap(int a,int b){ int c; c=a; a=b; b=c; printf("\nValues in the Function -->x=%d,y=%d",a,b);}
![Page 74: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/74.jpg)
Output
Enter value of x:5
Enter value of y:6
Values in the Function -->x=6,y=5
Values in the Main()-->x=5,y=6
![Page 75: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/75.jpg)
Call by reference
• Instead of passing value, the address of the argument will be passed.
• Any changes to the formal argument will affect the actual argument.
![Page 76: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/76.jpg)
Example#include <stdio.h>#include<conio.h>void main(){
int x,y,change(int*,int*);printf("\nEnter value of x:");scanf("%d",&x);printf("\nEnter value of y:");scanf("%d",&y);
![Page 77: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/77.jpg)
change(&x,&y);printf("\n\nValues in the Main()-->x=%d,y=%d",x,y);
}int change(int *a,int *b){ int c; c=*a;
*a=*b; *b=c; printf("\nValues in the Function -->x=%d,y=%d",*a,*b);}
![Page 78: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/78.jpg)
Output
Enter value of x:5
Enter value of y:6
Values in the Function -->x=6,y=5
Values in the Main()-->x=6,y=5
![Page 79: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/79.jpg)
Pointer to Array
• The elements of the array can also be accessed through a pointer.
• Exampleint a[3]={2,3,7};int *b;b=a;
![Page 80: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/80.jpg)
Example:#include<stdio.h>#include<conio.h>void main(){ int a[3]={2,3,7}; int *b; b=a; printf("\n The Value of a[0] = %d",a[0]); printf("\n The Address of a[0] = %u",&a[0]); printf("\n The Value of b = %d",b);}
![Page 81: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/81.jpg)
8744 2
9000
b a[0]
8744
Variable
Value
Address
![Page 82: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/82.jpg)
Output
The Value of a[0] = 2
The Address of a[0] = 8744
The Value of b = 8744
![Page 83: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/83.jpg)
Example#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]); } }
![Page 84: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/84.jpg)
2 3 7 9 10
a[0] a[1] a[2] a[3] a[4]
8724 8726 8728 8730 8732
Array
Value
Address
![Page 85: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/85.jpg)
Output The Value of a[0] = 2 The Address of a[0] = 8724 The Value of a[1] = 3 The Address of a[1] = 8726 The Value of a[2] = 7 The Address of a[2] = 8728 The Value of a[3] = 9 The Address of a[3] = 8730 The Value of a[4] = 10 The Address of a[4] = 8732
![Page 86: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/86.jpg)
Example#include<stdio.h>#include<conio.h>void main(){ int a[5]={1,2,3,4,5}; int i,sum=0; int *b; b=a; for(i=0;i<5;i++) { sum=sum + *b; b++; //b=b+1 } printf("\n The Sum is %d",sum);}
![Page 87: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/87.jpg)
Output
The Sum is 15
![Page 88: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/88.jpg)
Pointer and Structures
• Syntax:
struct structure_name
{
structure element1;
structure element2;
…………………….
}variable,*ptr;
![Page 89: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/89.jpg)
• Example:struct stud{
int sno;char name[10];int mark;
};
struct stud *s;
![Page 90: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/90.jpg)
Example#include<stdio.h>#include<conio.h>struct stud
{int regno;char name[10];int m1;int m2;int m3;
};struct stud s;struct stud *t;
![Page 91: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/91.jpg)
void main(){ float tot,avg; t=&s;printf("\nEnter the student regno,name,m1,m2,m3:");scanf("%d%s%d%d
%d",&s.regno,&s.name,&s.m1,&s.m2,&s.m3); tot=s.m1+s.m2+s.m3; avg=tot/3; printf("\nThe student Details are:"); printf("\n%d\t%s\t%f\t%f",s.regno,s.name,tot,avg); printf("\n%d\t%s\t%f\t%f",t->regno,t->name,tot,avg); }
![Page 92: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/92.jpg)
Output
Enter the student regno,name,m1,m2,m3:1aaa768976
The student Details are:1 aaa 241.000000 80.3333361 aaa 241.000000 80.333336
![Page 93: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/93.jpg)
Command Line Argument
• It allows the user to pass some information to the program while running the program.
![Page 94: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/94.jpg)
Example#include<stdio.h>#include<conio.h>void main(int argc,char argv[]){ printf("\n The Argument is %s",argv[0]); getch();}
![Page 95: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/95.jpg)
Output
C:\tc>a The Argument is C:\TC\A.EXE
![Page 96: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/96.jpg)
String Palindrome#include<stdio.h>#include<conio.h>#include<string.h>void main(){ char s1[15],s2[15]; printf("\nenter the string:"); scanf("%s",s1); strcpy(s2,s1); strrev(s1);
![Page 97: UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649f4f5503460f94c70682/html5/thumbnails/97.jpg)
if(strcmp(s1,s2)==0)printf("\n The string is palindrome");
else printf("\n The string is not a palindrome");
getch();}
Output:
enter the string: aba
The string is palindrome