Documenting My CPP Journey Part #5
Published: 2023-03-26 15:47:36

---
title: Documenting My CPP Journey Part #5
published: true
date: 2023-01-13 01:28:35 UTC
tags: 
canonical_url: https://theamazingatharva.hashnode.dev/documenting-my-cpp-journey-part-5
---

# **Introduction :**

Welcome to my first series on #Hashnode where I will be documenting my journey of learning C++ **from scratch to advance** to develop my programming skills and later make some **interesting projects**. In this blog, I will be sharing what I learnt in C++ since I wrote my **previous blog**. In this tutorial, we can study Functions withinside the C++ language. [**Do check out my previous blog**](https://dev.to/atharva_salitri/documenting-my-cpp-journey-part4-1na9-temp-slug-5315753) to be up to date with what I will be sharing in this one.

# Functions :

A function is a group of instructions that **accepts inputs, performs a certain calculation, and outputs the results.** The concept is to group similar or often performed actions into a function so that we **may call the function rather than writing the same code again** for various inputs. A function is a section of code that **executes only when it is called** , to put it simply.

There are two types of functions:

1. **Standard Library Functions:** Predefined in C++

2. **User-defined Function:** Created by users

The **syntax** to declare a function is:

```
returnType functionName (parameter1, parameter2,...) 
{
    // function body   
}

```

Here's an example of a function declaration.

```
// function declaration
void print() 
{
    cout << "Hello World";
}

```

Here,

- the name of the function is `print()`

- the return type of the function is `void`

- the empty parentheses mean it doesn't have any parameters

- the function body is written inside `{}`

### **Calling a Function**

In the above program, we have declared a function named `print()`. To use the `print()` function, we need to call it.

Here's how we can call the above `print()` function.

```
int main() {

    // calling a function   
    print(); 

}

```

### **Example 1: Display a Text**

```
#include <iostream>
using namespace std;

// declaring a function
void print() 
{
    cout << "Hello there!";
}

int main() 
{
    // calling the function
    print();

    return 0;
}

```

**Output :**

```
Hello there!

```

# **Why Are Functions Required?**

Functions assist us in minimising redundant code. If a software feature needs to be performed in several places, we build a function and call it everywhere rather than writing the same code repeatedly. This aids in maintenance as all changes must be made in one location should the functionality change in the future. Code is made modular using functions. Think of a large file with numerous lines of code. If the code is broken up into functions, it becomes incredibly easy to read and utilise. Abstraction is offered via functions. For instance, we don't have to worry about how library functions operate inside when we use them.

# Topics You Should Cover In Functions

1. Function Parameters

2. return statement:

3. **Function Prototype** : The function declaration code should appear before the function call in C++. However, we must utilise the function prototype if we want to define a function after the function call.

4. **C++ Library Functions** : In C++ programming, library functions are equivalent to built-in functions. Instead of writing their functions, programmers can use library functions by calling them directly. The C++ library functions sqrt(), abs(), isdigit(), etc. are some examples.

5. **Recursion** : Recursion is a method in C++ which calls itself directly or indirectly until a suitable condition is met. In this method, we repeatedly call the function within the same function, and it has a base case and a recursive condition.

# **Congratulations!**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1671164357055/oOvOXucPp.gif)

### You just learnt about Functions in C++ !!!

Thank you for joining me in my quest to conquer C++. In the next blog, well be talking more about Arrays in a C++ program, see you there, till then keep coding and keep learning.

# Credit To The Following Resource I Referenced :

1. [C++ Arrays](https://www.programiz.com/cpp-programming/arrays)

2. [GFG](https://www.geeksforgeeks.org/functions-in-cpp/)