Learn the basics of C++ functions with beginner-friendly examples. Understand what a function is, why functions are used, function definitions and calls, and how to execute reusable code.
After completing this lecture, students will be able to:
Imagine that you want to prepare tea.
The procedure is:
You follow the same procedure whenever you prepare tea.
Now imagine that you need to prepare tea five times. Would you write all these instructions again every time?
No. You can define the procedure once and use it whenever needed.
The same concept is used in C++ programming.
A function is a reusable block of code that performs a specific task.
Consider a program that displays a welcome message five times.
Example 1: Without using functions
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome to C++!" << endl;
cout << "Welcome to C++!" << endl;
cout << "Welcome to C++!" << endl;
cout << "Welcome to C++!" << endl;
cout << "Welcome to C++!" << endl;
return 0;
}
Output:
Welcome to C++!
Welcome to C++!
Welcome to C++!
Welcome to C++!
Welcome to C++!
Solution: We can use a function.
Example 2: Using a function
#include <iostream>
using namespace std;
void displayMessage()
{
cout << "Welcome to C++!" << endl;
}
int main()
{
displayMessage();
displayMessage();
displayMessage();
displayMessage();
displayMessage();
return 0;
}
Output:
Welcome to C++!
Welcome to C++!
Welcome to C++!
Welcome to C++!
Welcome to C++!
Notice that we wrote the message only once but used it five times.
This is called code reusability.
Consider this function:
void displayMessage()
{
cout << "Welcome to C++!";
}
Function structure
Return type
Specifies that the function does not return a value.
Function name
The name used to identify and call the function.
Parentheses
Used to specify parameters, if required.
Function body
Contains the statements that perform the task.
1. Return type – void
It specifies what type of value a function returns.
For now, understand:
void: Does not return a value.int: Returns an integer.float: Returns a decimal number.double: Returns a decimal number with greater precision.We will study return types in Lecture 2.
2. Function name – displayMessage
The function name identifies the function.
Naming rules are similar to variable naming rules:
3. Parentheses – ()
Parentheses are used to specify parameters.
In this example, there are no parameters.
4. Function body – { }
The function body contains the statements that execute when the function is called.
These are two important concepts.
A function definition contains the actual code that performs a task.
void displayMessage()
{
cout << "Hello Students!";
}
A function call executes the function.
displayMessage();
Comparison:
| Function definition | Function call |
|---|---|
| Defines the task | Executes the task |
| Contains statements | Uses function name |
| Normally written once | Can be called multiple times |
Consider:
#include <iostream>
using namespace std;
void display()
{
cout << "Hello Students!" << endl;
}
int main()
{
cout << "Program Started" << endl;
display();
cout << "Program Ended" << endl;
return 0;
}
Output:
Program Started
Hello Students!
Program Ended
Execution sequence:
main().Program Started.display() function call.display() function.Hello Students!.main() after the function finishes.Program Ended.Important: Program execution starts from main(), not from the first function definition written above it.
A C++ program can contain multiple functions.
Example:
#include <iostream>
using namespace std;
void displayWelcome()
{
cout << "Welcome to Ghazi University!" << endl;
}
void displayDepartment()
{
cout << "Department of CS & IT" << endl;
}
int main()
{
displayWelcome();
displayDepartment();
return 0;
}
Output:
Welcome to Ghazi University!
Department of CS & IT
Notice that each function performs a separate task.
This makes the program organized and easier to understand.
| Benefit | Explanation |
|---|---|
| Reusability | Write code once and use it multiple times. |
| Modularity | Divide a large program into smaller parts. |
| Readability | Make programs easier to understand. |
| Debugging | Find and fix errors more easily. |
| Maintenance | Modify one function without rewriting the entire program. |
| Incorrect | Correct |
|---|---|
void display |
void display() |
display |
display() |
Missing { } |
Use function body braces |
Calling display() before its declaration or definition |
Declare a prototype or define it before calling |
Writing function definition inside main() |
Define ordinary functions outside main() |
Question 1: What is a function?
Question 2: Why do programmers use functions?
Question 3: What is the difference between a function definition and a function call?
Question 4: Does a function execute automatically when it is defined?
Question 5: Can we call the same function multiple times?
Ask students to solve these problems independently. Do not provide the solution initially.
Problem Statement 1: Display Welcome Message
Write a C++ program using a function named displayMessage() that displays:
Welcome to Programming Fundamentals!
Tasks:
main().Requirements:
void return type.Problem Statement 2: Display Student Information
Write a C++ program using two separate functions to display student information.
Tasks:
displayName().displayDepartment().main().Requirements:
void return type.Problem Statement 3: Display a Number Pattern
Write a C++ program using a function named displayNumbers() that displays numbers from 1 to 10.
Tasks:
Requirements:
for loop.void return type.Lecture 1 quiz
0/5 answered
1. Where does the execution of a C++ program start?
A. First function defined
B. main()
C. display()
D. iostream
2. What does void mean?
A. Returns an integer
B. Returns a decimal
C. Does not return a value
D. Calls another function
3. Which part contains the actual statements of a function?
A. Function name
B. Parentheses
C. Function body
D. Return type
4. What is used to execute a function?
A. Function call
B. Function definition
C. Header file
D. Variable declaration
5. Can a function be called five times?
A. Yes
B. No
C. Only twice
D. Only inside loops
Submit quiz
Students should remember these five points:
main().