Welcome back...
This tutorials is about creating a function and returning its variable.
- Function -
- Mainly created if the same task needs to be done multiple times.
- Created to perform a definite task, then return the value of the function/calculations done to the main function which calls this one.
- It is used to make the code look clean and better. It is also easier to code. !!
- Return types -
- It is an important part of the writing the function.
- When the task to be performed is completed, the value is to be returned to the one calling it.
- Return types help to return the values.
Working of a function -
As said earlier, the 'void loop' is the main function which runs the task continuously for infinite time. But sometimes, there may be one or more tasks that are needed to be performed many times in one run. So a function is created in which, the task to be performed is written. Now, on completion of the task the value has to be returned to the main function for continuing the looping operation. So there are return types used for the returning of the variables.
Eg -
Let, I want a function that adds two numbers and returns the result for further use.
int addition (int a, int b)
{
int s;
s = a +b;
return (s);
}
1. int -- datatype of data to be returned - Can be int, long, double or void (if no return).
2. addition -- function name
3. (int a, int b) -- parameters used. The values of 'a' and 'b' were given previously by some other function. In this function only those values will be used.
4. return (s) - Return datatype should match the declaration datatype. ('int' in this case)
Calling of the function -
For execution of the function, it should be called. The below example shows how to call a function.
Eg -
Extending the above example, let the calling function mention the values of the two variables. Then call the addition function which adds the values and returns the sum to the calling function.
void loop()
{
int s = 10;
int r = 20;
int t = addition(s,r);
Serial.println(t);
}
int addition (int a, int b)
{
int c;
c = a +b;
return (c);
}
1. int t = addition(s,r) --- This statement -
- Creates a variable t of type int
- Calls the function addition, sending the two values of 's' & 'r'.
- The variables are added in addition and result is returned back to 't' .
2. Serial.println(t) ---- This statement prints the value of 't' ie 30
IMPORTANT POINTS -
- For every function there has to be a 'return' statement, if the function is returning a value.
- The calling function must have a variable initialized to store the value returned when a function is called.
- The function sholud always be outside the main function. ('loop' in this case)
- If a function is to be stopped midway, a ' return(-1) ' statement can be used.
- If a return statement is used in a function, midway, then the codes after the return statement are not executed.
- Eg -
int addition (int a, int b)
{
int c;
c = a +b;
return (c);
int d; // This line of code will never be executed.
d = a * b; // This line of code will never be executed.
Serial.println(d); // This line of code will never be executed.
}
Thank u .....
Comments and suggestions are welcome.... !!!
No comments:
Post a Comment