Download - Computer Programming- Lecture 11
![Page 1: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/1.jpg)
1TCP1231 Computer Programming I
Lecture 11Functions and
Pointers
![Page 2: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/2.jpg)
2TCP1231 Computer Programming I
Objectives
To learn more about functions
Explore how to declare and manipulate pointers with arrays, structure, and functions.
To become familiar with pointers.
To illustrate the applications of pointer variables and pointer operators.
To understand what is pointer arithmetic
![Page 3: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/3.jpg)
3TCP1231 Computer Programming I
• How can we return more than one values from a function?
• How can we write a function that modifies the values pass to it as parameters?
• We also know that when we pass an arrays to a function, the function may change the values stored in the array, but how does it work actually?
The Need for Pointers
![Page 4: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/4.jpg)
4TCP1231 Computer Programming I
a [ int ]int a;
If I want to store the address of a, what do I do?
int* p;
To store an integer, we can store it in an integer data type (int).
To store an address, then we must have some kind of data type specially for storing address.
p [ int* ]
Introduction to Pointers00000001
00000010
00000011
00000100
00000111
00001010
00001001
00001011
00001110
00001111
00000101
00000111
00001111
00000000
00000101
00001000
00010000
Value Addr
![Page 5: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/5.jpg)
5TCP1231 Computer Programming I
a [ int ]
p [ int* ]
How to Declare Pointers00000001
00000010
00000011
00000100
00000111
00001010
00001001
00001011
00001110
00001111
00000101
00000111
00001111
00000000
00000101
00001000
00010000
Value Addr
int a=2;
int * p;
p= &a;
2
00000010
This symbol is called ampersand, it is an operator which returns the address
The address of a is stored here
The value of variable p is the address of variable a.
Note: Since p is a variable, it has its own address too.
![Page 6: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/6.jpg)
6TCP1231 Computer Programming I
a
How to Declare Pointers00000001
00000010
00000011
00000100
00000111
00001010
00001001
00001011
00001110
00001111
00000101
00000111
00001111
00000000
00000101
00001000
00010000
Value Addr
int a=2;
int * p;
p= &a;
2
00000010p is called a pointer, just as an address points to a physical location
p actually points to the value of variable a.
p is pointing to the value of variable a, to access (retrieve or to modify the value) the value of variable a indirectly using the pointer, we can use a special operator called indirection operator (or dereference operator)
p
![Page 7: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/7.jpg)
7TCP1231 Computer Programming I
00000001
00000010
00000011
00000100
00000111
00001010
00001001
00001011
00001110
00001111
00000101
00000111
00001111
00000000
00000101
00001000
00010000
int a=2;
int * p;
p= &a;
cout << a << *p;
a= 5;
cout << a << *p;
a [ int ]
p [ int* ]
Value Addr
2
00000010
2 22 2
5 5
2 5
Here we are accessing the value of “a” indirectly through the pointer, thus the '*' operator is called: (indirection operator or dereferencing operator) as we are accessing the value by referencing another variable (pointer).
![Page 8: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/8.jpg)
8TCP1231 Computer Programming I
00000001
00000010
00000011
00000100
00000111
00001010
00001001
00001011
00001110
00001111
00000101
00000111
00001111
00000000
00000101
00001000
00010000
int a=2;
int * p;
p= &a;
cout << a << *p;
a= 5;
cout << a << *p;
*p= 7;
cout << a << *p;
a [ int ]
p [ int* ]
Value Addr
2
00000010
22 2
5 5
2 527 &
2 2
5 5
7 7
![Page 9: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/9.jpg)
9TCP1231 Computer Programming I
Note:
int main()
{
int *p;
*p = 10;
}
Where does the value 10 end up? Since we did not initialize p to point to anything, the value in p is undetermined and unpredictable, the above code may end up corrupting some crucial part of the operating system and it may hang your system. Therefore always save your programs regularly when programming with pointers, if you make some mistake, it may very well hang your whole system.
p [ int* ] 00000001
00000010
00000011
00000100
00000111
00001010
00001001
00001011
00001110
00001111
00000101
00000111
00001111
00000000
00000101
00001000
00010000
Value Addr
???
?
![Page 10: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/10.jpg)
10TCP1231 Computer Programming I
int *p1, *p2, m, n, *q;
is equivalent to
int *p1;
int *p2;
int m,n;
int *q;
int n;
int *p = &n; is equivalent to
int n;
int *p;
p = &n;
int* p1, p2;
is equivalent to
int *p1;
int p2;
int n;
int *p = n;
The C++ compiler will give you a syntax error saying this is invalid conversion. In C, the compiler will most likely give you a warning only
![Page 11: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/11.jpg)
11TCP1231 Computer Programming I
#include <iostream>using namespace std;int main() { int a= 2;; int b= 3; int c= 4; int *p1= &a; int *p2= &b; int *p3= &a;
cout << a << b << c << *p1 << *p2 << *p3 << endl;
a= a * 3; c= *p1; *p2= *p1 + a; cout << a << b << c << *p1 << *p2 << *p3 << endl;
p2= p1; c= 1; cout << a << b << c << *p1 << *p2 << *p3 << endl;
p3=p1; *p2= c; cout << a << b << c << *p1 << *p2 << *p3 << endl; return 0;}
a b c *p1 *p2 *p32 3 4 2 3 26 12 6 6 12 66 12 1 6 6 61 12 1 1 1 1
![Page 12: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/12.jpg)
12TCP1231 Computer Programming I
#include <iostream>using namespace std;
void swap( int A, int B ) { int temp; temp = A; A = B; B = temp;}
int main() { int a=2, b=7; cout << a << b << endl; swap( a, b ); cout << a << b << endl; return 0;}
a b2 72 7
#include <iostream>using namespace std;
void swap( int *A, int *B ) { int temp; temp = *A; *A = *B; *B = temp;}
int main() { int a=2, b=7; cout << a << b << endl; swap( &a, &b ); cout << a << b << endl; return 0;}
a b2 77 2
#include <iostream>using namespace std;
void swap( int &A, int &B ){ int temp; temp = A; A = B; B = temp;}
int main() { int a=2, b=7; cout << a << b << endl; swap( a, b ); cout << a << b << endl; return 0;}
a b2 77 2
Call by value Call by Pointer Call by Reference
![Page 13: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/13.jpg)
13TCP1231 Computer Programming I
#include <iostream>using namespace std;
int main() { int a=2, b=7; int * p1= &a;
*p1= a * b; cout << a << '\t' << b << '\t‘ << *p1 << endl;
delete p1; system(“pause”); return 0;}
Delete
![Page 14: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/14.jpg)
14TCP1231 Computer Programming I
Pointers
and Array
#include <iostream>using namespace std;
int main() { int i, b[] = {10,20,30,40}; int *bPtr = b; //pointing to the first elements of array cout << "The first element : " << *bPtr <<endl; cout << "The 3rd element : " << *(bPtr + 2)<< endl; //accessing array elements using pointer cout << "Accessing array elements using pointer : " << endl; for(i=0; i < 4; i++){ cout << bPtr[i] <<" "; } cout << endl; system("PAUSE"); return 0; }
![Page 15: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/15.jpg)
15TCP1231 Computer Programming I
10 20 30 40b[]
bPtr
bPtr = b;
bPtr points to the firstelement in array b[]
10 20 30 40b[]
bPtr + 2
*(bPtr + 2)
bPtr moves two to the 3rd element in array b[]
![Page 16: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/16.jpg)
16TCP1231 Computer Programming I
10 20 30 40b[]
bPtr
for(i=0; i < 4; i++){ cout << bPtr[i] <<" "; }
i = 1, bPtr[1]
10 20 30 40b[]
bPtr[i]
Output
20
i = 0, bPtr[0]
10
i = 2, bPtr[2]
30
i = 3, bPtr[3]
40
![Page 17: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/17.jpg)
17TCP1231 Computer Programming I
Dynamic arraysint main() { int *a; int size; cout<<"Enter the size of the dynamic array==>>"; cin >> size;
a = new int[size]; cout<<" Enter "<<size<<" value"<<endl; for(int i=0; i<size; i++) cin>>a[i]; cout<<"The values you have entered are :"<<endl; for(int i=0; i<size; i++) cout<<a[i]<<" "; delete [] a;
system(“pause”); return 0;}
![Page 18: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/18.jpg)
18TCP1231 Computer Programming I
More on Functions
![Page 19: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/19.jpg)
19TCP1231 Computer Programming I
Inline Functions• The use of macros(#) in C allows short “functions” to be called without
the normal overhead associated with function calls
• There are several characteristics of macros that makes their use unsuitable for C++
• Thus, C++ introduced the notion of inline functions to allow users to avoid the overhead of function calls
• With inline functions, the compiler controls the process (as opposed to the preprocessor, as was the case in C)
• How function calls work– Recall that the result of compiling a computer program is a set of
machine instructions (an executable program)– When the program is run, the OS loads the instructions into memory
(associating each instruction with a memory address) and then executes the instructions in order
– When a function call is encountered, the program “jumps” to the address of the function and then “jumps” back when the function has completed
![Page 20: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/20.jpg)
20TCP1231 Computer Programming I
• How functions work– Each time the program “jumps” to execute a function, there
is associated overhead:• “Loading” the function:
– The instruction immediately following the function call is stored
– The function arguments are copied to a reserved region of the stack
– Load the instruction referenced by the function call• Terminating the function call:
– (Possibly) store a return value in a register– Load the return instruction stored when the function
was first called
With inline functions, the compiler replaces each instance of the function call with the corresponding code
![Page 21: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/21.jpg)
21TCP1231 Computer Programming I
#include <iostream>using namespace std;
inline int add( int A, int B ) { return A+B;}
int main() { int a=2, b=7; for (int i=1; i<=5; i++) cout << add(a,b) << endl; system ("pause"); return 0;}
#include <iostream>using namespace std;
int add( int A, int B ) { return A+B;}
int main() { int a=2, b=7; for (int i=1; i<=5; i++) cout << add(a,b) << endl; system ("pause"); return 0;}
99999
![Page 22: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/22.jpg)
22TCP1231 Computer Programming I
Function Overloading
• Function overloading (aka function polymorphism) allows functions in C++ to share the same name
• For example, imagine that we have a sorting algorithm that we wish to use to implement functions for several types, such as int and char
• Traditionally, we would have to use different names for these functions
• Overloaded functions are distinguished by their argument list
void sortint(int a[]) {…}void sortchar(char a[]) {…}
void sort(int a[]) {…}void sort(char a[]) {…}
![Page 23: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/23.jpg)
23TCP1231 Computer Programming I
#include <iostream>using namespace std;
int add( int A, int B ) { return A+B;}int add( int A ) { return A;}int add( float A, float B ) { return A+B;}
int main() { int i1=2, i2=7; float f1=3, f2=5;
cout << add(i1, i2) << endl; cout << add(i1) << endl; cout << add(f1, f2) << endl; system ("pause"); return 0;}
928
![Page 24: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/24.jpg)
24TCP1231 Computer Programming I
Recursion
A function definition may contain a call to the function being defined.
#include <iostream>using namespace std;
int f( int a) { if (a<1) return 1; else return a * f(a-1); }
int main() { int k=5; cout << f(k); return 0;}
![Page 25: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/25.jpg)
25TCP1231 Computer Programming I
Function Headers#include <iostream>using namespace std;
int sum(int x, int y) { int s; s=x+y; return s;}void print(int x) { cout << x;}int main () { int v; v=sum (3, 5); print(v); return 0;}
#include <iostream>using namespace std;
int sum(int , int );void print(int );
int main () { int v; v=sum (3, 5); print(v);
return 0;}
int sum(int x, int y) { int s; s=x+y; return s;}void print(int x) { cout << x;}
![Page 26: Computer Programming- Lecture 11](https://reader036.vdocuments.us/reader036/viewer/2022081502/555c44f6d8b42a2c068b5027/html5/thumbnails/26.jpg)
26TCP1231 Computer Programming I
The End