Pointers and Functions
Pointers and Functions
(LAB-01)
Functions and Pointers - Recap
Table of Contents
1. Introduction 3
4. Concept Map 4
4.1 Reusability/ Modularity of Code 4
4.2 Function Prototype and Function Definition 4
4.3 Function Arguments and Parameters 4
4.4 Returning a Value from a Function 5
4.5 Pass by Value and Pass by Reference 6
4.6 Pointers 6
7. Practice Tasks 10
7.1 Practice Task 1 10
7.2 Practice Task 2 10
7.3 Practice Task 3 10
7.4 Practice Task 4 10
7.5 Outcomes 10
7.6 Testing 11
9. Evaluation criteria 11
Although this is a solution, it is not a practical one because it causes the code to become lengthy, unmanaged, poorly
structured and difficult to understand. In such a situation function handling offers an attractive and easy to implement
alternative that is widely accepted and promoted by programmers of all languages.
This lab is a refresher course on the use of functions, pointers and building logic. The functions that are designed will
receive variables and arrays from the user. The function will perform some processing on the provided data and return
results back to the main program.
The importance of this lab can be highlighted through the fact that functions play a pivotal role in the establishment
of classes, objects and their manipulations. A large portion of object oriented programming is based on manipulating
variables using functions. In the later portion the use of pointers has been emphasized. Concepts relating to pointers,
memory access, using arrays with pointers have been presented.
Lectures: 1, 2
Textbook: Object-Oriented Programming Using C++, Fourth edition, Robert Lafore.
o Chapters: 5, 10
4. Concept Map
Computer programmers have always devised mechanisms through which they can make their code more
understandable and then reusable. In this regard functions offer a very popular and easy mechanism of introducing the
above goals. Function handling operates on the concept of services. We can call the services of a function by providing
the required data and in result getting the promised service from the function.
Reusability of code means devising methods through which you can use code again and again without having to copy-
paste in the source file. Modularity of code means dividing the code into small, manageable and easy to understand
segments.
The creation of a function is based on two similar yet distinct statements called a function definition and function
prototype.
A function prototype explains only how a function will be called and what data type it will return. A function definition
on the other hand matches the function prototype and also includes a function body.
For example the following is the function prototype of a function that finds the sum of two integers passed to it:
int c=a+b;
In the following example, variables a and b are parameters of the function addtwo( ).
int c=a+b;
cout<<c;
Now suppose we are calling the same function from within the main().
void main( )
addtwo(3,4);
To increase the practical use of functions, a programmer may want the result of a function to be given back after
processing. This process is called returning a value from a function. It is important to remember that functions can
only return a single value (of any data type). If a function will not return a value then it is necessary to write void
before the function name in the prototype and the definition. It is not necessary for a function to return a value.
For example the following function does not return a value hence the void keyword is used
int c=a+b;
cout<<c;
The following function returns an integer value hence the keyword int is used.
int c=a+b;
return (c);
The value being returned can be displayed by using the following statement from where the function is being called.
cout<<addtwo(x, y);
All the functions we have discussed until now are pass by value. Pass by value is an argument passing technique in
which the function receives a parameter and works with a copy of the value being provided. This means if a change
is made to the parameter value then still no change will not be reflected in the argument.
On the other hand pass by reference is an argument passing technique in which the function works with the exact
variable that is being passed as an argument. This means that even the smallest change in a parameter will be exactly
reflected in the arguments. This further implies that the arguments and the parameters are tightly coupled.
4.6 Pointers
Pointers are special kind of variables that are allowed to hold the address of another variable. Because of their pointing
ability pointers are considered very powerful in C++. Pointers can hold the address of another variable and this is
called referencing the memory location. When we attempt to extract values from a memory location then this is called
dereferencing a pointer.
In the figure provided above there is a pointer variable called a. This variable is pointing to the memory address of
variable b. The memory address of variable b is 1008. In this diagram you will also note that the pointer a has its
own memory address. This is a very important fact because pointers themselves also require a space in memory. When
we write a code on our compilers remember that every computer has its own memory and the availability of memory
space. Our compilers take the help of a memory manager and allocate space in a memory slot that is available to the
system. This means every time we run the code we may get a different memory allocation.
Consider the code provided below:
Figure 2: The various operations that can be performed with a pointer. Output is also provided. Memory addresses
may be different depending on the hardware environment
In the code above first a simple integer variable is created. Then an integer pointer is created because we intend to
point to an integer variable. The pointer is then given the address of variable x by using the address operator. Then
we use the reference operator (*) that directs us to the memory location where the pointer is pointing to.
5.2.1 Task-1
Write a program to find the force if the mass and the acceleration of a body is given. Write a function to perform the
calculation. Your task is to provide the mass and acceleration as arguments to the function and then display the force
without returning a value.
5.2.2 Task-2
Write a program that creates a function to find the area of a cylinder if the radius and height of the cylinder is provided.
The function should return the value of area. The area of a cylinder is 𝜋 𝑟2ℎ
6.1 Tools
Visual Studio 2013\2019.
In the code above note that the two functions use different mechanisms for passing/ using an array. You can choose
the method that suits your code/ situation. The size is passed in both functions because without the size the functions
can easily cross the array bounds. Hence passing the array size is a good practice.
6.3.2 Compilation
After writing the code, compile your code according to the guidelines mentioned. Remove any errors and warnings
that are present in your code.
7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You need to finish the
tasks in the required time. When you finish them, put these tasks in the following folder:
\\dataserver\assignments$\OOP\Lab01
7.5 Outcomes
After completing this lab, students will be able to use functions and also understand the concept of parameters,
arguments and returning of values. Students should also be comfortable with pointers and their use in functions.
University of Management and Technology Page
Department of Computer Science, SST 10
Functions and Pointers - Recap
7.6 Testing
Test Cases for Practice Task-1
Sample Inputs Sample Outputs
Number: 3 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, …
Number: 5 120
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each task is assigned
marks which will be evaluated by the instructor in the lab depending on the accomplishment of the assigned tasks.
10.1 Books
- Object-Oriented Programming Using C++, Fourth edition, Joyce Farrell