# Creating a library

I created a simple library for formatting text output on the serial monitor. It has thee functions, one that creates carriage returns, one that sets the number of spaces between characters and the last one prints dashes. I followed the tutorial on the Arduino.cc site. The problem I have is that I can’t pass the value to each of the functions when I call them in the code. If you look at the screenshot from the sketch when I create an instance of nFormat it requires me to put a value in and it passes that value to each of the functions (lines, spaces, dashes). I want to be able to specify the value directly when I call the function. See the second red line on the output screenshot. I know this post is a bit long I would appreciate it if someone could point me in the right direction.

nFormat.h

``````#ifndef nFormat_h
#define nFormat_h

#include "Arduino.h"

class nFormat
{
public:
nFormat(int counter);

void lines();
void spaces();
void dashes();

private:
int _counter;
};

#endif
``````

nFormat.cpp

``````#include "Arduino.h"
#include "nFormat.h"

nFormat::nFormat(int counter)
{
_counter=counter;
}

void nFormat::lines()
{
for (int x = 0; x < _counter; x++)
{
Serial.println(" ");
}

}

void nFormat::spaces()
{
for (int x = 0; x < _counter; x++)
{
Serial.print(" ");
}
}

void nFormat::dashes()
{
for (int x = 0; x < _counter; x++)
{
Serial.print("-");
}
`````` • Why are you using a class if you want to be able to pass all the parameters directly to the functions? May 25 '16 at 4:30
• I want to avoid having to make an instance of nFormating everytime I want to change the value of the number of spaces or dashes. For example if I wanted to have 10 dashes instead I would have to create nFormat nFormatTwo(10); I get an error when I compile the program when placing the value inside the function call. May 25 '16 at 4:35

If you want to specify the number of spaces/dashes/... every time you call any of these methods, you just have to make that number a parameter of the method:

``````void lines(int count)
{
for (int x = 0; x < count; x++)
Serial.println();
}
``````

Then, your class does not need its private `_counter`. Then, since there is no data in the class, you do not need to instantiate it: just make all the methods `static` and call the methods as `nFormat::method_name()`. And since you are not making instances, you do not need a constructor either. And since the methods are really trivial, there is no point in implementing them in a .cpp file: you can just inline them in the class declaration. Combining all this gives this small demo program:

``````class nFormat
{
public:
static void lines(int count)
{
for (int x = 0; x < count; x++)
Serial.println();
}

static void spaces(int count)
{
for (int x = 0; x < count; x++)
Serial.print(" ");
}

static void dashes(int count)
{
for (int x = 0; x < count; x++)
Serial.print("-");
}
};

void setup()
{
Serial.begin(9600);
nFormat::dashes(5);
nFormat::lines(5);
nFormat::spaces(5);
nFormat::dashes(5);
}

void loop() {}
``````

It could be noted than this class serves only to avoid putting all the functions in the global namespace. It is essentially a namespace. You could as well replace `class` by `namespace` (and remove the `public` keyword) to make that more explicit.
{"ft_lang_label":"__label__en","ft_lang_prob":0.752931,"math_prob":0.95662975,"size":1341,"snap":"2021-43-2021-49","text_gpt3_token_len":330,"char_repetition_ratio":0.13462977,"word_repetition_ratio":0.1091703,"special_character_ratio":0.27367637,"punctuation_ratio":0.17153284,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95574963,"pos_list":[0,1,2],"im_url_duplicate_count":[null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-07T15:35:31Z\",\"WARC-Record-ID\":\"<urn:uuid:0e073e9a-f7e5-42e1-a99f-158048c19e7e>\",\"Content-Length\":\"138311\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c2bc9c2e-4768-445b-86fb-b11023b7ac25>\",\"WARC-Concurrent-To\":\"<urn:uuid:65927142-8781-431f-9e07-0390599f5a25>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://arduino.stackexchange.com/questions/24560/creating-a-library\",\"WARC-Payload-Digest\":\"sha1:CB6JMM4J2JUWS4GUASAPA3GS7CU7TQ3G\",\"WARC-Block-Digest\":\"sha1:VJFWOBKKNXELBFSXM4RWZYVX6XORV7UO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964363400.19_warc_CC-MAIN-20211207140255-20211207170255-00166.warc.gz\"}"}