Lec Functions and Pointers
Lec Functions and Pointers
Functions
1) Call by value.
The call by value method of passing arguments to a function copies
the actual value of an argument into the formal parameter of the
function. In this case, changes made to the parameter inside the
function have no effect on the argument.
// function declaration
void swap(int x, int y);
int main () {
// local variable declaration:
int a = 100;
int b = 200;
return 0;
}
// function definition to swap the values.
void swap(int x, int y) {
int temp;
temp = x; /* save the value of x */
x = y; /* put y into x */
y = temp; /* put x into y */
}
Remarks on function
11 Local variables
Known only in the function in which they are defined
All variables declared inside a function are local variables
Parameters
Local variables passed to function when called (passing-
parameters)
Global variables
Variables defined outside and before function main.
Can be accessible and used anywhere in the entire program
The rand( ) function will generate the same set of random numbers each
time you run the program .
To force NEW set of random numbers with each new run use the
randomizing process.
Randomizing is accomplished with the standard library function
srand(unsigned integer);which needs a header file <stdlib.h>
Example on Random function
14 generator num entered
by user
#include<iostream >
#include<stdlib.h>
using namespace std;
int main()
{
int i;
int num ;
// we will enter a different number each time run
cin >> num;
srand (num);
for( i=1; i<=5; i++)
cout << 1+rand()%6;
return 0;
}
Recursive (Recursion function)
15
Recursion and Recursive Functions
The main() can call another function…..normal
A function calls another function2….normal
A function calls itself ?! Possible?? YES
A recursive function is one that call itself.
16 Recursive (Ex. factorial)
#include<iostream >
using namespace std;
int factorial( int n );
void main()
{ int n;
cout<<"\nEnter the Value of n \n";
cin>>n;
cout<<"\nFactorial => "<<factorial (n);
}
#include <iostream>
using namespace std;
int main(){
cout<<sum(3);
return 0;
}
Recursive (Ex. Adding numbers)
22
#include <iostream>
using namespace std;
int main(){
cout<<sum(3);
return 0;
}
23
Pointers
24 Points to be covered:
Why pointers?
What is a pointer?
Pointer variable initialization and declaration.
Pointers operators.
Pointers and arrays.
25
Why pointers?
Why we need Pointers?
#include <iostream>
using namespace std;
int main () {
int var1;
char var2[10];
return 0;
}
Pointer variable initialization
29 and declaration
A pointer is a variable whose value is the address of
another variable. Like any variable or constant, you must
declare a pointer before you can work with it. The general
form of a pointer variable declaration is :
type *var-name; or type* var-name; or type*var_name;
(space in definition doesn’t matter)
Pointers initialization
Initialized to 0, NULL, or an address.
0 or NULL points to nothing.
Pointer operators
30 & (address operator)
returns the address of variable stored in memory.
Example
Pointer operators
31
* (indirection/dereferencing operator)
Returns the value of what its operand points
*yPtr returns value of variable y
(because yPtr points to y).
* can be used to assign a value location in memory
*yPtr = 7; // changes y to 7
* and & are inverses
Cancel each other out
*&myVar == myVar
&*yPtr == yPtr
Pointer variable initialization
32 and declaration
int x, num = 25; int *ptr ; // declaration of variable x , num ,
and pointer ptr
ptr = & num ; // ptr holds address and points to
value stored in num
*ptr = 44; // Store 44 into the memory cell
pointed by ptr
x = * ptr ; // Store the value pointed by ptr to variable x
cin >> * ptr; // Store value into the memory cell that
ptr points to
Pointer variable initialization
33 and declaration
There are important operations, which is done with the pointers
very frequently:
#include <iostream>
using namespace std;
int main () {
int var = 20; // actual variable declaration.
int *ip; // pointer variable
ip = &var; // store address of var in pointer variable
cout << "Value of var variable: ";
cout << var << endl;
// print the address stored in ip pointer variable
cout << "Address stored in ip variable: ";
cout << ip << endl;
// access the value at the address available in pointer
cout << "Value of *ip variable: ";
cout << *ip << endl;
return 0;
}
35
Pointer variable assignment
36
Pointers Arithmetic
Some arithmetic operators can be used with pointers
#include <iostream>
#include <iostream>
int main () {
int var[MAX] = {10, 100, 200};
return 0;
}
40
Pointers and arrays
Array of pointers: there may be a situation, when we want to
maintain an array, which can store pointers to an int or char or
any other data type available. Following is the declaration of
an array of pointers to an integer
int *ptr[MAX];
return 0;
}
Dynamic array (Dynamic
42
memory allocation)