Learn with Yasir

Share Your Feedback

Introduction to Functions in C++


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.

1. Learning objectives

After completing this lecture, students will be able to:

  1. Explain what a function is.
  2. Understand why functions are needed.
  3. Identify function definition and function call.
  4. Write a simple function in C++.
  5. Execute a function multiple times.
  6. Understand the basic structure of a function.

2. Introduction: A real-life example

Imagine that you want to prepare tea.

The procedure is:

  1. Boil water.
  2. Add tea leaves.
  3. Add milk and sugar.
  4. Boil the mixture.
  5. Serve the tea.

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.

3. Why do we need functions?

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++!
  • Why are we writing the same statement five times?
  • What if we need to display this message 100 times?
  • Can we write this statement once and use it whenever required?

Solution: We can use a function.

4. Our first C++ 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.

5. Understanding the structure of a function

Consider this function:

void displayMessage()
{
    cout << "Welcome to C++!";
}

Function structure

void

Return type

Specifies that the function does not return a value.

displayMessage

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.

Explanation of each component

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:

  • Must not contain spaces.
  • Cannot start with a number.
  • Cannot be a C++ reserved keyword.
  • Should describe the function’s purpose.

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.

6. Function definition and function call

These are two important concepts.

Function definition

A function definition contains the actual code that performs a task.

void displayMessage()
{
    cout << "Hello Students!";
}

Function call

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

7. How does a function execute?

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:

  1. Program starts from main().
  2. Displays Program Started.
  3. Encounters display() function call.
  4. Control moves to the display() function.
  5. Displays Hello Students!.
  6. Returns to main() after the function finishes.
  7. Displays Program Ended.

Important: Program execution starts from main(), not from the first function definition written above it.

8. Multiple functions in a program

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.

9. Benefits of functions

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.

10. Common beginner mistakes

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?

12. Practical lab exercises

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:

  1. Define the function.
  2. Call the function from main().
  3. Display the message three times.

Requirements:

  • Use void return type.
  • Do not use parameters.

Problem Statement 2: Display Student Information

Write a C++ program using two separate functions to display student information.

Tasks:

  1. Create a function named displayName().
  2. Create a function named displayDepartment().
  3. Call both functions from main().

Requirements:

  • Use void return type.
  • Do not use parameters.

Problem Statement 3: Display a Number Pattern

Write a C++ program using a function named displayNumbers() that displays numbers from 1 to 10.

Tasks:

  1. Define the function.
  2. Use a loop inside the function.
  3. Call the function twice.

Requirements:

  • Use a for loop.
  • Use void return type.

13. Quick knowledge check

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

14. Lecture summary

Students should remember these five points:

  1. A function is a reusable block of code.
  2. Every function has a name and a body.
  3. A function definition describes what the function does.
  4. A function call executes the function.
  5. Program execution starts from main().