Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

Pointers & Functions

WEEK # 16
LECTURE 1

Pointers and Arrays (Revision)


#include<iostream.h> void main() { int a[3]={4,7,11};

cout<<&a[0]<<endl; cout<<a<<endl<<endl;; cout<<&a[1]<<endl; cout<<(a+1)<<endl<<endl; cout<<&a[2]<<endl; cout<<(a+2)<<endl<<endl; }

Pointers and Arrays (Revision)


#include<iostream.h> void main() { int a[3]={4,7,11};

cout<<a[0]<<endl; cout<<*a<<endl<<endl;; cout<<a[1]<<endl; cout<<*(a+1)<<endl<<endl; cout<<a[2]<<endl; cout<<*(a+2)<<endl<<endl; }

Pointers and Arrays (Revision)


#include<iostream.h> void main() { int a[3]={4,7,11}; int *p; p=a; cout<<*(p+0)<<endl; cout<<p[0]<<endl<<endl;; cout<<*(p+1)<<endl; cout<<p[1]<<endl<<endl; cout<<*(p+2)<<endl; cout<<p[2]<<endl<<endl; }

Pointers and Arrays (Revision)


#include<iostream.h> void main() { int a[3]={4,7,11}; int *p; p=a; cout<<(p+0)<<endl; cout<<&p[0]<<endl<<endl;; cout<<(p+1)<<endl; cout<<&p[1]<<endl<<endl; cout<<(p+2)<<endl; cout<<&p[2]<<endl<<endl; }

Pointers and Arrays (Revision)


#include<iostream.h> void main() { int a[3]={4,7,11}; int *p; p=a; cout<<&a[0]<<endl; cout<<p<<endl<<endl;; p++; cout<<&a[1]<<endl; cout<<p<<endl<<endl;

Pointers and Arrays (Revision)


#include<iostream.h> void main() { int a[3]={4,7,11};

cout<<&a[0]<<endl; cout<<a<<endl<<endl;; a++; cout<<&a[1]<<endl; cout<<a<<endl<<endl;

Error Address of Array are fixed and cant be changed like Pointer

PASSING ARGUMENT TO FUNCTIONS

Functions of Factorial
#include <iostream.h> int factorial(int num); void main() { int x; int factorial(int num) { int product=1; for(int i=1;i<=num;i++) { product=product*i; } return product; }

cout<<"Enter number to find Factorial : "; cin>>x; cout<<"Answer is "<<factorial(x); cout<<endl<<endl; }

Passing Argument To Functions


There are two ways that a language can pass an argument to a function. The first is call-by-value. This method copies the value of an argument into the parameter of the function. Therefore, changes made to the parameter of the subroutine have no effect on the argument used to call it. Second is Call-by-reference In this method, the address of an argument (not its value) is copied into the parameter. Inside the function, this address is used to access the actual argument specified in the call. This means that changes made to the parameter will affect the argument used to call the function

Call-by-Value
#include<iostream.h> double reciprocal(double x); void main() { double t=10; cout<<"Reciprocol of 10 is "<<reciprocal(t)<<endl; cout<<"Value of t is "<<t<<endl; } The only thing modified is the local variable x. The local double reciprocal(double x) variable t used as an argument { will still have the value 10 and x=1/x; is unaffected by the operations return x; inside the function. }

Call-by-Value
#include<iostream.h> double reciprocal(double x); void main() { double x=10; cout<<"Reciprocol of 10 is "<<reciprocal(x)<<endl; cout<<"Value of x is "<<x<<endl; } double reciprocal(double x) { x=1/x; return x; }

Call-by-Reference
It is possible to manually create a call-by-reference by passing the address of an argument (that is, a pointer) to a function. Pointers are passed to functions just like any other values. Of course, it is necessary to declare the parameters as pointer types.

Call-by-Reference
#include<iostream.h> void fun(int *j); void main() { int i; int *p; p=&i; fun(p); cout<<i<<endl; void fun(int *j) { *j=100; } }

Call-by-Reference
#include<iostream.h> void fun(int *j); void main() { int i=10; int *p; p=&i; cout<<i<<endl; fun(p); cout<<i<<endl; void fun(int *j) { *j=100; } }

Call-by-Reference
#include<iostream.h> void fun(int *j); void main() { int i=10; cout<<i<<endl; fun(&i); cout<<i<<endl; void fun(int *j) { *j=100; } }

Call-by-Reference
#include<iostream.h> void fun(int *j); void main() { int i=10; cout<<i<<endl; fun(&i); cout<<i<<endl; void fun(int *i) { *i=100; } }

Call-by-Value
#include<iostream.h> double reciprocal(double x); void main() { double t=10; cout<<"Reciprocol of 10 is "<<reciprocal(t)<<endl; cout<<"Value of t is "<<t<<endl; } The only thing modified is the local variable x. The local double reciprocal(double x) variable t used as an argument { will still have the value 10 and x=1/x; is unaffected by the operations return x; inside the function. }

Call-by-Reference
#include<iostream.h> void reciprocal(double *x); void main() { double t=10; reciprocal(&t); cout<<"Value of t is "<<t<<endl; } void reciprocal(double *x) { *x=1/(*x); }

Factorial (Call-by-Value)
#include <iostream.h> int factorial(int num); void main() { int x; int factorial(int num) { int product=1; for(int i=1;i<=num;i++) { product=product*i; } return product; }

cout<<"Enter number to find Factorial : "; cin>>x; cout<<"Answer is "<<factorial(x); cout<<endl<<endl; }

Factorial (Call-by-Reference)
#include <iostream.h> void factorial(int *num); void main() { int x; cout<<"Enter number to find Factorial : "; cin>>x; } factorial(&x); cout<<"Answer is "<<x; cout<<endl<<endl; } void factorial(int *num) { int product=1; for(int i=1;i<=(*num);i++) { product=product*i; } *num=product;

You might also like