Passing strings to a function

I'm learning how to pass variables and strings to a function. Apparently, I have much to learn .. especially when it comes to passing strings.

My code compiles error free, but at run time, the strings don't appear .... and seem to be ignored. Any help will be appreciated. Are passing strings to functions especially difficult to master?

Here's my code.

void setup() {
Serial.begin(9600);
}

void loop(){
float val;
float oz;
float gal;
String value;
String ounces;
String gallons;
oz=10;
gal=20;
val=30;
PRNT(oz,gal,val,value,ounces,gallons);
}

void PRNT(float oz,float gal,float val, String value, String ounces, String gallons){
delay(500);
Serial.print(val);
Serial.print(gallons);
Serial.print("= ");
Serial.print(oz);
Serial.print(ounces);
Serial.println(val);
Serial.print(ounces);
Serial.print("= ");
Serial.print(gal);
Serial.print(gallons);

}

You didn't assign any value to these:

String value;
String ounces;
String gallons;

Therefore there is nothing to print.

Do you mean like this?

String = "value";
String = "ounces";
String = "gallons";

??

More like:

String value="value";
String ounces="ounces";
String gallons="gallons";

However it seems like a lot of effort when you could just do:

Serial.print("value");
Serial.print("ounces");
Serial.print("gallons");

Yes but the PRNT function will be called at other times, passing e.g. value, inches. centimeters.

The sketch is designed to do unit conversion between various types of units e.g. length, area, volume … with sub units e.g. VOLUME: pints <—> quarts.

I’m trying to use just one function (PRNT) to print out any requested conversion.

ardocman:
Yes but the PRNT function will be called at other times, passing e.g. value, inches. centimeters.

The sketch is designed to do unit conversion between various types of units e.g. length, area, volume … with sub units e.g. VOLUME: pints <—> quarts.

I’m trying to use just one function (PRNT) to print out any requested conversion.

No problem. You could do it the following way then you don’t use String:

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  float val;
  float oz;
  float gal;
  oz = 10;
  gal = 20;
  val = 30;
  PRNT(oz, gal, val, "value", "ounces", "gallons");
}

void PRNT(float oz, float gal, float val, char* value, char* ounces, char* gallons) 
{
  delay(500);
  Serial.print(val);
  Serial.print(gallons);
  Serial.print("=  ");
  Serial.print(oz);
  Serial.print(ounces);
  Serial.println(val);
  Serial.print(ounces);
  Serial.print("=  ");
  Serial.print(gal);
  Serial.print(gallons);
}
[/code

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

And for a small Arduino program in the small memory of a microprocessor I reckon it is better to declare the cstrings as global variables and allow the function to access them that way.

...R

By the way, you seem to be having a little trouble with your code tags. You have managed to put a complete set before your code and after your code hence the two little boxes with no code in them. The correct way to do it is to copy your code, hit the code tag button and then paste. Don't move the cursor just hit ctrl V. You could sort your current situation by editing your post and deleting the first [/códe] and the second [códe] so you only have one box with your code in it.

[códe]Your code goes here [/códe]

OK, I'll try to remember that.

Robin2:
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino.

But, if you are going to use them and you are going to pass them to functions, then do so by Reference or Pointer. Passing by value will make a new (temporary) String object on the stack that is copy-constructed from the original argument. This includes allocating the memory necessary for the string’s characters on the heap. This will just exacerbate the problems of the String class related to dynamic allocation.

ToddL1962:
No problem. You could do it the following way then you don't use String:

void setup() 

{
 Serial.begin(9600);
}

void loop()
{
 float val;
 float oz;
 float gal;
 oz = 10;
 gal = 20;
 val = 30;
 PRNT(oz, gal, val, "value", "ounces", "gallons");
}

void PRNT(float oz, float gal, float val, char* value, char* ounces, char* gallons)
{
 delay(500);
 Serial.print(val);
 Serial.print(gallons);
 Serial.print("=  ");
 Serial.print(oz);
 Serial.print(ounces);
 Serial.println(val);
 Serial.print(ounces);
 Serial.print("=  ");
 Serial.print(gal);
 Serial.print(gallons);
}
[/code

Thanks Todd, char* works!

My understanding of char was that it only applied to a single character. I didn't see char* mentioned anywhere in the Arduino language reference. Very useful to know.

The Arduino language is C++. You can reference any C++ documentation or tutorials. char* is a pointer to a character or character string.

Got it .... thanks.

I’m still having trouble passing some semblance of strings into a Function.

My PRNT function is intended to receive calculation results from one selected unit conversion function out of many , and print out those results. The PRNT function is intended to reduce code by eliminating the need for multiple prints for each and every unit conversion function. PRNT prints the conversion values OK, but NOT the conversion names.

Here’s my simplified code that prints results from one conversion function.

I’m sorry, but I still can’t figure out how to use the </> begin-end code markers ??

float val;
float inch;
float cm;
float oz;
float gal;
char ounces;
char gallons;

void setup() {
Serial.begin(9600);
}
void loop(){
val=10;
oz=20;
gal=30;
PRNT (val,oz,gal,ounces,gallons);
}

void PRNT(float, float, float, char, char){
delay(500);
Serial.println(val);
Serial.println(oz);
Serial.println(gal);
Serial.println(ounces);
Serial.println(gallons);
Serial.println();
delay(2000);
}
//The sketch compiles OK. But RUN prints only val,oz,and gal; but NOT ounces or gallons ??

The sketch compiles OK

I'm surprised. I'll have to try it here. Okay...

You never assigned any value to ounces or gallons. So they will have the default value of zero.
This is wrong, I can see where your muddled thinking is leading but you need to declare the parameters:

void PRNT(float val, float oz, float gal, char ounces, char gallons){

Then, when you refer to say, 'oz', it will the value you passed to the function.

A char initialized to zero, is an unprintable character. I think you are probably dreaming about what a char is, but if not, you at least have to initialize it to something you can see when you print it:

char ounces = 'o';

What I see here is programming by guesswork, instead of looking at examples and documentation.

Don't capitalize function names. By convention, that is for pre-processor macros.

A C string declaration would look like this:

char ounces[] = "hello, I am an ounce.";
// yes you can print me!
Serial.println(ounces);

You can see an array declaration there, it's because C strings are arrays of char.

I suggest you revise your program so that it at least creates and prints a string successfully. Post it, and I or someone else will help you pass it to a function.

Much better to have 3 separate functions if you ask me…

void printOunces(float oz);
void printGallons(float gal);
void printValue(float val);

Then you can hard code the units and don’t need to pass the string in at all.

Thanks aarg.

I used your char ounces[] = "hello, I am an ounce."; example, and all's well.

ardoman