codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

18 / 87 topics
18Function Basics & User-defined Types19Function Parameters20Inline Functions21Function Overloading22Scope23Recursion24Lambda Expressions
Tutorials/C++ Programming/Function Basics & User-defined Function Types
⚡C++ Programming

Function Basics & User-defined Function Types

Updated 2026-05-12
45 min read

Function Basics & User-defined Function Types

In this tutorial, we will explore the basics of functions in C++ and delve into the various types of user-defined functions. Functions are fundamental building blocks in programming that allow you to encapsulate code into reusable units. This makes your programs more organized, easier to read, and maintain.

Introduction

Functions in C++ are blocks of code designed to perform a specific task. They can take inputs (parameters), process them, and return outputs. Understanding how to declare, define, and call functions is essential for writing efficient and modular code. In this tutorial, we will cover the basic syntax of functions, different types of user-defined functions, and best practices.

Core Content

Function Declaration and Definition

A function in C++ consists of two parts: declaration and definition.

  • Declaration: This tells the compiler about the function's name, return type, and parameters. It is also known as a prototype.
  • Definition: This contains the actual implementation of the function.

Example 1: Basic Function Declaration and Definition

C++
1#include <iostream>
2using namespace std;
3
4// Function declaration
5int add(int a, int b);
6
7int main() {
8 int result = add(5, 3);
9 cout << "The sum is: " << result << endl;
10 return 0;
11}
12
13// Function definition
14int add(int a, int b) {
15 return a + b;
16}
Output

Return Types

The return type of a function specifies the type of value it returns to the caller. If a function does not return any value, you use the void keyword.

Example 3: Void Function

C++
1#include <iostream>
2using namespace std;
3
4// Void function that prints a message
5void printMessage(string message) {
6 cout << "Message: " << message << endl;
7}
8
9int main() {
10 printMessage("Hello, World!");
11 return 0;
12}
Output

Best Practices

  • Descriptive Function Names: Use meaningful names that describe the function's purpose.
  • Single Responsibility Principle: Each function should perform a single task.
  • Avoid Long Functions: Break down complex tasks into smaller, manageable functions.
  • Consistent Naming Conventions: Follow consistent naming conventions for better readability.

Practical Example

Let's create a simple program that calculates the area of different shapes using user-defined functions.

C++
1#include <iostream>
2using namespace std;
3
4// Function to calculate the area of a rectangle
5double rectangleArea(double length, double width) {
6 return length * width;
7}
8
9// Function to calculate the area of a circle
10double circleArea(double radius) {
11 const double PI = 3.14159;
12 return PI * radius * radius;
13}
14
15int main() {
16 double length, width, radius;
17
18 cout << "Enter length and width of rectangle: ";
19 cin >> length >> width;
20 cout << "Area of rectangle: " << rectangleArea(length, width) << endl;
21
22 cout << "Enter radius of circle: ";
23 cin >> radius;
24 cout << "Area of circle: " << circleArea(radius) << endl;
25
26 return 0;
27}
Output
Enter length and width of rectangle: 5 10
Area of rectangle: 50
Enter radius of circle: 7
Area of circle: 153.938

In this example:

  • We define two functions: rectangleArea and circleArea.
  • The user is prompted to enter dimensions, and the corresponding area is calculated using these functions.

Summary

ConceptDescription
Function DeclarationTells the compiler about the function's name, return type, and parameters.
Function DefinitionContains the actual implementation of the function.
Function CallingUsing the function name followed by parentheses to execute it.
Return TypesSpecifies the type of value returned by a function.
Void FunctionsFunctions that do not return any value.
User-defined FunctionsCustom functions created by the programmer to perform specific tasks.

What's Next?

In the next tutorial, we will explore Function Parameters in detail, including how to pass arguments by value and by reference, as well as default parameter values. Understanding function parameters is crucial for writing flexible and efficient code.

Stay tuned!


PreviousJump Statements: break, continue, gotoNext Function Parameters

Recommended Gear

Jump Statements: break, continue, gotoFunction Parameters