wk 16 l111111

21
WEEK # 16 LECTURE 1 ³Pointers & Functions´

Upload: rizvi098

Post on 07-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 1/21

WEEK # 16

LECTURE 1

³Pointers & Functions´

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 2/21

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;

}

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 3/21

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;

}

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 4/21

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;

}

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 5/21

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;

}

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 6/21

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;

}

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 7/21

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

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 8/21

PASSING ARGUMENT TOFUNCTIONS

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 9/21

Functions of Factorial

#include <iostream.h>

int factorial(int num);

void main()

{int x;

cout<<"Enter number to find Factorial : ";

cin>>x;

cout<<"Answer is "<<factorial(x);

cout<<endl<<endl;

}

int factorial(int num)

{

int product=1;

for(int i=1;i<=num;i++)

{product=product*i;

}

return product;

}

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 10/21

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 intothe 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

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 11/21

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;

}

double reciprocal(double x)

{

x=1/x;

return x;}

The only thing modified is the

local variable x. The local

variable t used as an argument

will still have the value 10 and

is unaffected by the operations

inside the function.

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 12/21

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;}

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 13/21

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.

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 14/21

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;

}

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 15/21

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;}

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 16/21

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;

}

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 17/21

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;

}

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 18/21

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;

}

double reciprocal(double x)

{

x=1/x;

return x;}

The only thing modified is the

local variable x. The local

variable t used as an argument

will still have the value 10 and

is unaffected by the operations

inside the function.

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 19/21

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);

}

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 20/21

Factorial (Call-by-Value)

#include <iostream.h>

int factorial(int num);

void main()

{int x;

cout<<"Enter number to find Factorial : ";

cin>>x;

cout<<"Answer is "<<factorial(x);

cout<<endl<<endl;

}

int factorial(int num)

{

int product=1;

for(int i=1;i<=num;i++)

{product=product*i;

}

return product;

}

8/6/2019 Wk 16 L111111

http://slidepdf.com/reader/full/wk-16-l111111 21/21

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;

}