0% found this document useful (0 votes)
14 views5 pages

Pointers Midterm Exam 2024 2025 COMPUTER PROGRAMMING 2

The document covers key concepts in computer programming, focusing on user-defined functions, parameter passing mechanisms, recursion, and arrays in C++. It explains how to declare and invoke functions, different types of parameters, and various methods for passing parameters. Additionally, it introduces recursion as a technique for solving problems and provides examples of array declarations and usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Pointers Midterm Exam 2024 2025 COMPUTER PROGRAMMING 2

The document covers key concepts in computer programming, focusing on user-defined functions, parameter passing mechanisms, recursion, and arrays in C++. It explains how to declare and invoke functions, different types of parameters, and various methods for passing parameters. Additionally, it introduces recursion as a technique for solving problems and provides examples of array declarations and usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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.

How user-defined functions and parameters work


Declaration - A function declaration includes the keyword "Function", the function name, and
a list of input parameters.
Invocation - A function call includes the function name and a list of parameters in special
bracket notation.
Parameters - A function can have zero or more input parameters. Parameters can be scalar or
tabular.
Return value - A function can return a scalar value or a table.

Examples of user-defined functions


In SQL Server, user-defined functions can be defined as part of the query or stored in
the database metadata.
In C, user-defined functions can pass arguments to the function and return a value.
In IBM, user-defined functions can encapsulate and reuse functionality in policies.

C++ Functions
A function - is a block of code that performs a specific task.

There are two types of function:


1. Standard Library Functions (Predefined in C++)
2. User-defined Function (Created by users)

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.

C++ Function Declaration


The syntax to declare a function is:
returnType functionName (parameter1, parameter2,...) {
// function body
}

Here's an example of a function declaration.


// function declaration

void greet() {
cout << "Hello World";
}

- the name of the function is greet()


- the return type of the function is void
- the empty parentheses mean it doesn't have any parameters
- the function body is written inside {}

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.

Two types of parameters


 Formal Parameters: Variables used in parameter list of a function as placeholders. Also called only
parameters.
 Actual Parameters: The expressions or values passed in during a function call. Also
called arguments.

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.

5. Pass by Value-Result: A mix of pass-by-value and pass-by-result.

6. Pass by Name: Uses textual substitution of argument names.

The Scope of an Object


The SCOPE of an object is the part of the program over which that object is recognized.

The scope of an object may be


 Statement scope– A single statement.
 Block scope– A block within a function.
 Function scope– A function.
 Class scope– A class (to be discussed soon).
 Global scope– The entire program.
An object cannot be accessed outside of its scope

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.

FUNCTIONS WITH EMPTY PARAMETERS

#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;

void add(int aa, int bb){


cout<<aa+bb;
}

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:

string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};


1. string = data type
2. cars = variable name
3. 4 = number of elements
4. “Volvo” = element
5. , = separator

Array Examples:
#include<iostream>
using namespace std;

int main(){
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];

return 0;
}

You might also like