C++ from Scratch to Advance / Functions in C++ / Types of Functions in C++
Home /C++ from Scratch to Advance /Functions in C++ /Types of Functions in C++

Types of Functions in C++

In our journey of how to learn C++ step by step, we’ve already discovered that functions are the “labors” of our program. But not all labors are the same! Some come pre-hired by the C++ language, while others are specialists you build yourself.

This guide breaks down the different Categories and Types of functions to help you write cleaner, more professional code.

Definitions

Let’s define our roadmap for today in the simplest terms:

Term Description
Function Name The unique label used to identify and call a specific task.
Arguments The input values or “raw materials” you provide to a function so it can do its work.
Pre-defined Functions Ready-made functions that are already built into C++ libraries.
User-defined Functions Custom functions that you write yourself to solve specific problems.
Void Function A function that performs an action but does not send a value back to the main program.
Value-returning Function A function that completes a task and then “returns” a result to the place it was called.

 

The Concept: Functions as Mathematical Machines

However, we discussed the functions in the previous lesson, but let’s recap the concept of the functions in C++ with another example.

If you remember math class, you’ve seen functions like f(x)=x².

  • The is the name.
  • The is the input (the argument).
  • The result (like if ) is the output.

In C++, functions work exactly the same way: they take an input, perform an operation, and usually provide an output.

Types of Functions

In C++ the functions are mainly divided into two categories:

Category 1: Pre-defined (Built-in) Functions

C++ is a powerful language that comes with many tools already “in the box.” These are stored in libraries (like iostream or cmath).
Example: The Power Function (pow) If you need to calculate x⁴, you could write . But C++ provides a pre-defined function called pow. You simply give it the arguments (the base and the exponent), and it returns the result.
  • The Benefit: You don’t have to write the complex code to calculate powers yourself; the language creators have already done it and stored it in a library for you

Category 2: User-defined Functions

The creators of C++ couldn’t predict every single task you might need. For unique needs—like calculating a specific student’s grade or a custom product discount—you must write your own functions.

Within user-defined functions, there are further main types:

  1. Void Functions (The “Doers”)

A void function is like a Post Office. You give it a letter (input), it delivers it (action), but it doesn’t give you anything back in return.

  • Example: A print function. You send it two numbers, it displays them on the screen using cout, and it’s done. It doesn’t send a value back to the main program to be used in another calculation.
  1. Value-returning Functions (The “Calculators”)

A value-returning function is like a Juice Blender. You put in fruit (input), it blends them (operation), and it hands you back a glass of juice (the result) to use.

  • Example: A sum function. You give it two numbers, it adds them, and returns the total. You can then take that returned total and use it in a division or another formula later in your program.

 Summary Table: Which Function Should You Use?

Feature Void Function Value-returning Function
Result Performs an action (like printing). Calculates and returns a result.
Keyword Uses void in the definition. Uses a data type (like int or float).
The “Return” No return value sent back. Uses a return statement to send data back.
Best For Displaying messages or updating screens. Mathematical calculations needed later.

 

Relatable Daily Life Example: The Kitchen

  • Pre-defined Function: An Oven. You didn’t build the oven; you just use it. You give it dough (input), and it performs the “Bake” function to give you bread.
  • Void User-defined Function: Cleaning the Counter. You perform the task, the counter is clean, but you aren’t “holding” a result to give to someone else. The task is finished where it happened.
  • Value-returning User-defined Function: Grating Cheese. You take a block of cheese (input), perform the task, and you are left with a bowl of shredded cheese (output) that you can now take to the stove to put on a pizza.

 

Interactive Elements: Try It Yourself!

  1. Reflection Prompt: Look at the main() function you’ve been using. Why do we usually see return 0; at the end? Based on today’s lesson, is main() a void or a value-returning function?
  2. Challenge 1: If you were writing a program for a car, would the “Display Speedometer” function be a void or value-returning function? Why?
  3. Challenge 2: Think of the pow(x, y) function. If you use it in your code, does it “return” a value, or is it a “void” function?

Engagement Boost: Join the Conversation!

  • What is your biggest C++ fear—is it understanding where the “returned” value actually goes?
  • Question for the comments: If you could create a “User-defined” function for your daily life, what would it be named?

For official technical documentation on built-in libraries, visit the ISO C++ Standard Library Reference.

Happy Coding!

Video Tutorial