Pointers Midterm Exam 2024 2025 COMPUTER PROGRAMMING 2
Pointers Midterm Exam 2024 2025 COMPUTER PROGRAMMING 2
MIDTERM
UNIT 1:
USER-DEFINED FUNCTIONS AND PARAMETERS (12 HOURS)
User-defined functions - are reusable pieces of code that can take input parameters and return a
result. Parameters are pieces of information that help the function perform its task.
C++ Functions
A function - is a block of code that performs a specific task.
A user-defined function group code to perform a specific task and that group of code is given a name or
what we called identifier.
When the function is invoked from any part of the program, it all executes the codes defined in the body
of the function.
void greet() {
cout << "Hello World";
}
UNIT 2:
PARAMETER PASSING MECHANISMS (12 HOURS)
PARAMETER PASSING
Parameters or arguments - data can be sent to functions when they are called in order to perform
operations.
There are 6 different methods using which we can pass parameters to a function in C++. These are:
Pass by Value
Pass by Reference
Pass by Pointer
Pass by Result
Pass by Value-Result
Pass by Name
1. Pass by Value
In pass by value method, a variable's value is copied and then passed to the function.
Example:
2. Pass by Reference
In pass-by-reference method, instead of passing the value of the argument, we pass the reference of
an argument to the function.
Example
3. Pass by Pointer
The pass-by-pointer – A pointer to the argument is passed to the function Example
4. Pass by Result: The formal parameter's value is sent back to the actual parameter after the function
call.
UNIT 3:
RECURSION AND FUNCTION CALL VISUALIZATION (12 HOURS)
1. Base Case: The function checks if n is less than or equal to 1. If true, it returns 1, as the factorial
of 0 and 1 is 1.
2. Recursive Case: If n is greater than 1, the function returns n multiplied by the factorial of n-1 .
Recursion
Recursion is the technique of making a function call itself. This technique provides a way to break
complicated problems down into simple problems which are easier to solve.
#include<iostream>
using namespace std;
void greet() {
cout << "Hello World \n";
}
int main(){
greet();
return 0;
}
FUNCTIONS WITH 2 (TWO) PARAMETERS
#include<iostream>
using namespace std;
int main(){
int a =1, b = 4;
add(a,b);
return 0;
}
UNIT 4:
ARRAYS SORTING LINEAR SEARCH BINARY SEARCH (12 HOURS)
C++ Arrays
Arrays - are used to store multiple values in a single variable, instead of declaring separate variables for
each value.
To declare an array, define the variable type, specify the name of the array followed by square
brackets and specify the number of elements it should store:
string cars[4];
We have now declared a variable that holds an array of four strings. To insert values to it, we can use an
array literal - place the values in a comma-separated list, inside curly braces:
Array Examples:
#include<iostream>
using namespace std;
int main(){
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
return 0;
}