0% found this document useful (0 votes)
22 views

Pointers and Functions

This document provides an overview of a lab manual on functions and pointers in C++. The objectives of the lab are to understand functions, parameters, arguments, passing values by value and reference, and using pointers. It includes a table of activities and time allotted. The concepts section defines key terms like reusability, function prototypes, parameters vs arguments, returning values, and passing by value vs reference. The document aims to refresh concepts around functions and pointers through practice tasks.

Uploaded by

Leo Kazim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Pointers and Functions

This document provides an overview of a lab manual on functions and pointers in C++. The objectives of the lab are to understand functions, parameters, arguments, passing values by value and reference, and using pointers. It includes a table of activities and time allotted. The concepts section defines key terms like reusability, function prototypes, parameters vs arguments, returning values, and passing by value vs reference. The document aims to refresh concepts around functions and pointers through practice tasks.

Uploaded by

Leo Kazim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lab Manual for Object Oriented Programming

(LAB-01)
Functions and Pointers - Recap

University of Management and Technology Page 1


Department of Computer Science, SST
Functions and Pointers - Recap

Table of Contents
1. Introduction 3

2. Activity Time boxing 3

3. Objective of the Experiment 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

5. Home Work Before Lab 7


5.1 Problem Solution Modelling 7
5.2 Practices from home 8

6. Procedure & Tools 8


6.1 Tools 8
6.2 Setting-up Visual Studio 2013 8
6.3 Walkthrough Task 8

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

8. Evaluation Task (Unseen) 11

9. Evaluation criteria 11

10. Further Reading 12


10.1 Books 12
10.2 Slides 12

University of Management and Technology Page 2


Department of Computer Science, SST
Functions and Pointers - Recap

Lab 01: Functions and Pointers - Recap


1. Introduction
C++ programming that you studied in the previous semester is purely based on writing a set of instructions in a
particular sequence such that the output is based on the sequence of the written statements. If this sequence of
instructions is modified then it could have a large impact on the resulting program. Often a situation arises where we
intend to use a particular chunk of code again and again in a single program. In this scenario using sequential
programming the programmer is forced to copy-paste the code in various locations of the source file.

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.

Relevant Lecture Material

 Lectures: 1, 2
 Textbook: Object-Oriented Programming Using C++, Fourth edition, Robert Lafore.
o Chapters: 5, 10

2. Activity Time boxing


Table 1: Activity Time Boxing
Task No. Activity Name Activity time Total Time
5.1 Evaluation of Design 15 mins 15 mins
6.2 Setting-up Visual Studio 5 mins 5 mins
6.3 Walkthrough Task 25 mins 25 mins
7 Practice tasks 15 + 15 + 20 + 35 (mins) 85 mins
9 Evaluation Task 50 min for two task 50 mins
Total Time 180 Minutes

3. Objective of the Experiment


After completing this lab the student should be able to:
 Understand the purpose/ advantage of creating a function.
 Understand the difference between a function definition and prototype.
 Understand the difference between a parameter and an argument.
 Understand how a function is created and called.
 Understand how values are passed by value and by reference to a function. Returning the values thereafter.
 Use pointers and manipulate variables using pointers.
 Use an array with pointers in a function.

University of Management and Technology Page 3


Department of Computer Science, SST
Functions and Pointers - Recap

4. Concept Map

4.1 Reusability/ Modularity of Code

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.

4.2 Function Prototype and Function Definition

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:

void addtwo (int, int);

The following is the function definition of the above defined function:

void addtwo (int a, int b)

int c=a+b;

cout<<”The sum is ”<<c;

4.3 Function Arguments and Parameters


There are two types of variables a function is related with; namely function arguments and function parameters.
Function arguments are those variables that are provided to a function. These variables are passed whenever a
function is called.
Function parameters are those variables that are created and initialized when parameters are passed to a function.
The scope of a function parameter is always within the body of the function.
The data type of arguments must match the parameters of a function.

In the following example, variables a and b are parameters of the function addtwo( ).

void addtwo (int a, int b);

University of Management and Technology Page 4


Department of Computer Science, SST
Functions and Pointers - Recap

int c=a+b;

cout<<c;

Now suppose we are calling the same function from within the main().

void main( )

int x=3, y=4;

addtwo(3,4);

4.4 Returning a Value from a Function

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

void addtwo (int a, int b);

int c=a+b;

cout<<c;

The following function returns an integer value hence the keyword int is used.

int addtwo (int a, int b);

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.

University of Management and Technology Page 5


Department of Computer Science, SST
Functions and Pointers - Recap

cout<<addtwo(x, y);

4.5 Pass by Value and Pass by Reference

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.

University of Management and Technology Page 6


Department of Computer Science, SST
Functions and Pointers - Recap

Figure 1: The working of a pointer (image courtesy of Wikipedia)

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. Home Work Before Lab

5.1 Problem Solution Modelling


Write the pseudo-code of the following task. You are required to bring this code with you and submit to your lab
instructor.
University of Management and Technology Page 7
Department of Computer Science, SST
Functions and Pointers - Recap

5.1.1 Problem description


Write pseudo-code for a program that will find the factorial of a number. Your task is to also perform checks on the
number entered by the user because the factorial can only be found of an integer and a positive number. Incorporate
these two conditions in you pseudo-code.

5.2 Practices from home

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. Procedure & Tools

6.1 Tools
Visual Studio 2013\2019.

6.2 Setting-up Visual Studio 2013 [Expected time = 5 mins]


Setup Visual Studio and make a project named “FindAverage”.

6.3 Walkthrough Task [Expected time = 25 mins]


Write a C++ program that creates (in main function) an array of type int having 6 indexes. Now write a function called
arr_avg( ) that will find the average of all the entries of the array. This function should return the average back to the
main. Also write another function called arr_display() that will display all the indexes of the array.

University of Management and Technology Page 8


Department of Computer Science, SST
Functions and Pointers - Recap

6.3.1 Writing Code


In the source file created in the project “FindAverage” write following C++ code:

Figure 3: Function for finding average of values in an array

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.

University of Management and Technology Page 9


Department of Computer Science, SST
Functions and Pointers - Recap

6.3.3 Executing the Program


A sample output after running the program is shown below. Also run the code with other possible inputs.

Figure 2: Final output of FindAverage program.

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.1 Practice Task 1 [Expected time = 15 mins]


Write a program that contains a function to find twenty odd numbers starting from a particular number provided by a
user.

7.2 Practice Task 2 [Expected time = 15 mins]


Write a program to find the power of a number if the base and exponent is provided in the main function. Use pass by
reference to compute the power. Your function should not return a value.

7.3 Practice Task 3 [Expected time = 20 mins]


Write a program that has an integer array having 20 indexes. The program should have a function that can receive the
array and then return the sum of all the indexes of the array. Use pointers to demonstrate the iteration through the
array.

7.4 Practice Task 4 [Expected time = 35 mins]


Write a program that finds the factorial of a non negative number provided by the user. Since the user can enter a
floating point number hence your input should accommodate floats and ints. To find the factorial of the entered number
you will need to design three functions as follows:
 Function to determine if a number is a whole number or not
 Function to determine if the number is positive or not
 Function to find the actual factorial
Remember that to find the factorial the number must of positive and a whole number. So if any of these conditions
are not met then you cannot determine the factorial.

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, …

Test Cases for Practice Task-2


Sample Inputs Sample Outputs
Base: 4 Result = 4194304
Exponent: 11
Check that the function works with pass by reference

Test Cases for Practice Task-3


Sample Inputs Sample Outputs
Array having all entries 1 Sum = 20

Check that pointers have been used to iterate the array

Test Cases for Practice Task-4


Sample Inputs Sample Outputs
Number: -5 Invalid Input

Number: 5.5 Invalid Input

Number: 5 120

Table 2: Confirmation of practice tasks T1, T2, T3, T4

Practice Tasks Confirmation


T1
T2
T3
T4

8. Evaluation Task (Unseen) [Expected time = 50 mins]


The lab instructor will assign you unseen tasks depending upon the progress of the students.

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.

University of Management and Technology Page


Department of Computer Science, SST 11
Exception Handling

Table 3: Evaluation of the Lab

Sr. Task No Description Marks


No.
1 4.1 Problem Modelling 20
2 6 Procedures and Tools 5
3 7.1 Practice task 1 with Testing 10
4 7.2 Practice task 2 with Testing 10
5 7.3 Practice task 3 with Testing 10
6 7.4 Practice task 4 with Testing 15
7 8.1 Evaluation Tasks (Unseen) 20
8 Good Programming 10
Practices
Total Marks 100

10. Further Readings

10.1 Books
- Object-Oriented Programming Using C++, Fourth edition, Joyce Farrell

University of Management and Technology Page 12


Department of Computer Science, SST

You might also like