CSC233-CSC211 2024 - 2025 Lecture 3
CSC233-CSC211 2024 - 2025 Lecture 3
Title
Introduction to Programming With C++
Third Edition
Author
Daniel Y. Liang
2
LECTURE OUTLINE
Defining a Function
Inline Functions
Calling a Function
Local, Global, and
Void Functions Static Local Variables
Passing Arguments by Passing Arguments by
Value Reference
Overloading Functions Constant Reference
Function Prototypes Parameters
Default Arguments
3
Instructions to Students:
Running the code examples
5
Defining a Function 6
Defining a Function
7
Defining a Function
The function that returns a value is called a value-
returning function and the function that does not return
a value is called a void function.
The variables declared in the function header are known
as formal parameters or simply parameters.
A parameter is like a placeholder.
When a function is invoked, you pass a value to the
parameter.
8
Defining a Function
This value is referred to as an actual parameter or
argument.
The parameter list refers to the type, order, and number
of the parameters of a function.
The function name and the parameter list together
constitute the function signature.
Parameters are optional; that is, a function may contain
no parameters.
For example, the rand() function has no parameters.
9
Defining a Function
The function body contains a collection of statements
that define what the function does.
The function body of the max function uses an if
statement to determine which number is larger and
returns the value of that number.
A return statement using the keyword return is required
for a value-returning function to return a result.
The function exits when a return statement is executed.
10
Calling a Function
11
int max(int num1, int num2)
{
int result;
if (num1 > num2)
result = num1; Define max function
else
result = num2; The main function
return result;
}
int main()
{
int i = 5;
int j = 2;
int k = max(i, j); // invoke the max function
cout << "The maximum between " << i << " and " << j << " is " << k << endl;
return 0;
}
13
Void Functions(Example 2)
14
// Print grade for the score // Main function definition
void printGrade(double score) int main()
{ {
if (score >= 70.0) cout << "Enter a score: ";
cout << 'A' << endl; double score;
else if (score >= 60.0) cin >> score;
cout << 'B' << endl; cout << "The grade is ";
else if (score >= 50.0) printGrade(score);
cout << 'C' << endl;
else if (score >= 45.0) return 0;
cout << 'D' << endl; }
else
cout << 'F' << endl;
}
16
Passing Arguments by Value – Cont’d
Note: You will not always be ‘lucky’ if you misarrange the arguments of a
function call, i.e. if the order does not match the parameter order, like in
the function call below.
nPrint(3, 'a')
19
// Return the max between two int values
int max(int num1, int num2){
if (num1 > num2)
return num1;
else
return num2;
}
return 0;
}
int main()
{
cout << "The maximum between 3.0 and 5.4 is " << max(5.4, 3.0) << endl;
return 0;
}
int main()
{
cout << "The length of the name is equal to " << length_of_name() <<
endl;
cout << "The length of the name is equal to " <<
length_of_name("Chinonso");
}
What are default Arguments (Example 5)? Observe the output for each function call
24
Inline Functions
25
inline void info_display(int year_of_adm, string name = "Steve")
{
cout << "Length of " << name << " is " << name.length();
cout << ". Admitted in the year " << year_of_adm;
}
int main()
{
string name = "Jemimah"; int adm_year = 2009;
info_display(adm_year);
cout << endl;
info_display(adm_year, name);
}
27
Local, Global, and Static Local
Variables(Example 7)
Demonstrates the scope of local and global variables
28
The Scope of Variables in a for Loop
29
Static Local Variables
After a function completes its execution, all its local
variables are destroyed.
These variables are also known as automatic variables.
Sometimes it is desirable to retain the values stored in
local variables so that they can be used in the next call.
C++ allows you to declare static local variables using the
keyword static.
Static local variables are permanently allocated in the
memory for the lifetime of the program.
30
void t1(); // Function prototype
int main()
{
t1(); // Call function t1
t1(); // Call function t1 again
return 0;
}
void t1()
{
static int x = 1; // x is permanently allocated in the memory for the lifetime of the
program.
int y = 1;
x++;
y++;
cout << "x is " << x << endl;
cout << "y is " << y << endl;
}
Static Local Variables - Demo 31
Passing Arguments by Reference
When you invoke a function with a parameter, as
described in the preceding sections, the value of the
argument is passed to the parameter. This is referred
to as pass-by-value.
Parameters can also be passed by reference, which
makes the formal parameter an alias of the actual
argument.
Thus, changes made to the parameters inside the
function are also made to the arguments.
32
void increment(int n)
{ void increment(int& n)
n++; {
cout << "The value of n inside the n++;
function is " cout << "The value of n inside the function is
<< n << endl; "
} << n << endl;
}
int main()
{ int main()
int x = 1; {
cout << "Before the call, x is " << x << int x = 1;
endl; cout << "Before the call, x is " << x << endl;
increment(x); increment(x);
cout << "After the call, x is " << x << cout << "After the call, x is " << x << endl;
endl;
return 0;
return 0; }
}
34
Constant Reference Parameters
If your program uses a pass-by-reference parameter
and the parameter is not changed in the function, you
should mark it constant to tell the compiler that the
parameter should not be changed.
To do so, place the const keyword before the
parameter in the function declaration.
Such a parameter is known as constant reference
parameter.
35
Constant Reference Parameters
For example, num1 and num2 are declared as constant
reference parameters in the following function and they
should not be changed within the function.
36
•Write a C++ function named sum that takes two integers as parameters and returns their sum. Use this function in main() to add two numbers input by the user.
Exercises (1)
1. Write a C++ function named ‘sum’ that takes two
integers as parameters and returns their sum.
Use this function in ‘main()’ to add two numbers input by the
user.
Exercises (2)
39
Exercises (3) – Cont’d 40