Today the whole classroom were ordered to write a code as a quiz that could calculate square and cube roots in a uncommon way where we used functions.
At the first of the class many of us didn’t know what to do, so we started to search information on the web. Finally, I asked for a classmate help and my wolrd lit up. This i what I learned from both internet and my friend:
- I need to call the library math.h to use mathematical operations like square root and cube root.
- To write a function you need to declare the type of this one.
- Then you have to write its name and, in parentheses, declare all the variables you need to use during the execution of the function.
- After that, in curly brackets, you write the type of the function, call the function, and tell it what to do.
- At the end of the function, you need to return the resultant value.
- Finally, you print as well as ask for data into int main() function. You can add all the conditionals or loops you need.
- For using the functions previouly created you only have to utilize a output command, and inside it you call the function with the corresponding variable. The format is something like this: cout << function_name(variable); .
Well, that is what I practically did today during the class, and I have to say that I was joyful when my program could be compiled.
P.D. I made my program to use imaginary numbers for square roots, with the symbol “i” (which means the square root of -1), when the user entered a negative number, instead of displaying an error, beacuse I think it’s m