Week 3-4
Week 3-4
• Functions
• Pointers
• You can invoke a function whenever you need to execute the operations it
defines.
• A function can have multiple inputs (called arguments) and generate a single
output (called return value)
Notes It is also possible to create a function without any arguments and/or return value
Structured programming or modular programming
Calling a function
Creating Functions
Even main()
cannot access
any variables
defined in any of
the other
functions, such
as add() and
sub()
Functions with Arguments
The values that we pass from the calling function to the called function are referred as
arguments.
Functions with Return Values
• The ‘called’ function can give a single value back to the ‘calling’ function.
• This value sent from the ‘called’ function to the ‘calling’ function is called
a ‘return value’
• The ‘return’ statement in C/C++ is used to send value back to the caller.
• Given below is the syntax of return statement: return ; The statement will return
the result of the expression back to the calling function.
Functions with Return Values
NOTES We can only return one value at a time from the ‘called’ function to the
‘calling’ function.
NOTE When return statement is encountered, it ends the execution of the
current function and transfers the control back to the caller immediately.
Execution of return statement
Program to find the maximum of six numbers using the function with two
arguments: An example
Passing Array as an Argument to the Function
finding the maximum of n elements from the array
Recursion
Recursive Function A function that calls itself conditionally is called a recursive
function
Unconditional recursion
Calculating xy using recursion: An example
values of x (base) and y (index) are taken as input from the user. We will create a recursive
function named exp() returns an integer value of xy.
let assume x = 2 , y = 4. We now need to calculate the result of 24 in the program.
The process of inserting elements into the stack is called as pushing whereas the
process of removing elements from stack is called as popping.
Activation frames: How Function Calls and Returns are Internally Handled in C/C++
Each time a function is called, space is created in memory for its execution. This
space is called an activation frame or an activation record.
1. Local variables These are variables declared within the scope of the function.
4. Return location This is the address of the statement of caller where the control of
execution should return after the called function ends.
Activation frames are used to handle function calls and returns by the CPU.
RAM Addresses
When a function is called, the activation frame for
the function is created and pushed on the stack
Stack storing activation frames of main(), f1() and f2()
Storage Classes in C/C++
The place in the memory where a variable is stored on its creation is called a
‘storage class’ of that variable.
syntax of declaring variables by specifying the ‘storage class’ for the variables
Auto storage class
This is the default storage class for all the variables of C/C++. The variables of type
auto are stored in the main memory (RAM) of the computer system
RAM is usually
splitted into
logical
partitions of
64 KB each.
Each logical
partition is
called
segment.
Storage of integer variable in RAM
Every location in RAM is of 1 byte, it requires two consecutive locations in RAM because
size of integer is of 2 bytes.
Creating Pointers
• This means that the value of the pointer will be the memory address of location
which stores the value of another variable.
• The variable whose address is contained in the pointer is called target variable.
• We can access the value of target variable by using a pointer that points to it, this
process is called indirection.
C/C++ supports usage of following operators for performing operations with the
address that is contained in the pointer:
1 0 02
All the elements of an array can be accessed by using a single pointer, if the pointer
stores the starting address of the array
Accessing array elements using pointer p
Notes
In general, *(p+i) will always give the same value as that of a[i] provided the
pointer p stores the starting address of the array
This works because it does not change the address in pointer a, but only stores
values in locations of the array.
Making re-initializations work with pointers
Works well
Following code will fail, because it applies string initialization to the array after its
creation
Works well
The type cast (int[5]) informs the compiler that the initializer type is an
array of 5 integers
NOTES
A string constant can be initialized to a
character pointer without casting it to array
type. This is because compiler anyway
creates an array of characters to store a
string.
As p is not a constant pointer, it is possible to reinitialize it to another string. For
example, the statement
NOTES When a pointer is initialized, compiler allocates the necessary memory
which stores the data, For example, the statement
if you do not initialize the pointer p and just declare it with a intention to accept a
string as input from the user, the program will crash
To resolve this issue we must reserve some memory space to store the input data,
The scanf() statement works well because the pointer p holds the starting address of
an 100 byte space in memory before using it to accept a string.
For example, if we initialize an integer pointer z as below:
compiler will allocate the memory of 5 integers. But, if you need to print each
data value pointed by z you must print it one element at a time
Similarly, instead of initializing z to an array if you wish to accept the array values
as input from the user you must follow the following two steps:
Self-addressability of Character Variables
If you try to print the address of a character you will actually get its value.
Characters and Strings in C/C++ are said to be self-addressable.
Prints the value $ instead of printing memory address of the character variable
So in summary, using control string as %d and & operator with character variable
will force C/C++ to print the address of a character variable.
If you are a C++ programmer and using cout statement you answer the question
below:
This is because the operator << is overloaded for character type of variables in C++; to
print the value of the variable instead of the address.
The below statement prints the address of the character c by typecasting it to an
integer type of pointer(int*)
For example, consider the statement below which creates an 1-D array p of
character pointers of size 3
We can initialize each of the individual pointers p[0],p[1], and p[2] to string values
The following loop will also print each of the individual strings
Pointer to a Pointer
A pointer that stores the address of another pointer is called a ‘Pointer to a Pointer’.
A ‘pointer’ which is pointed by a ‘pointer to pointer’ is called a target pointer and the
data item pointed by target pointer is called target variable.
Pointer to a Pointer
We can now create a pointer q such that, it stores the address of pointer p. Such a
pointer q is called a ‘pointer to a pointer’ because it points to another pointer p. Below
is the syntax for creating a ‘pointer to a pointer’:
Pointers and 2D Arrays
Array of pointers created before representing the matrix by the compiler
Address of individual elements in a matrix
Storage of a 2-D array in computer memory
We can access all the elements of a 2-D array using a single pointer to a pointer.
To illustrate this concept, we will create a 2-D pointer named as p
Applying indirection operator on address 1020 will fetch the contents of memory
locations 1020 which is an integer value 80.
Program to perform addition of 2 matrices using pointers: An example
void Pointers
void type pointers can point to a variable of any type void pointers are also called
universal pointers because they have an ability to point to data values of every type.
will make the void type pointer p to point to floating point variable a
attempt to copy the address stored in pointer p to pointer pint, we have to do a type
casting of a void pointer p to integer type as seen in the statement
The code below creates two variables a and b of type int and float, respectively. The
given program uses a void type pointer p to access the value of each of these variables.
Pointer to a Function
Pointers in C/C++ can also point to a function which is defined in the program. To make
a pointer point to a function, type of the pointer must match with the prototype
of the function.