This is bit of an odd question to ask, but could someone give me an example of multiple functions being used for the following. Like one function performs one thing and a second one does something else? Let's say function one does calculations and then the second one determines if the number calculated is odd or even. Really the two functions could be anything, I'm having a hard time understanding functions even though they are pretty simple lol. Could someone post a program just for an example that uses more than one function?
-
1Do you know how to write and use one function? Can you show us your code?– BetaMay 24, 2020 at 5:48
-
I do know how to use one function, but I am not able to show off my code because it's from an older assignment. What I want to do in my program with functions I cannot really explain because I'm afraid of plagiarizing hence why I wrote that the two functions could be anything you guys want, I just would like to see an example with more than just one function and that they are used interchangeably. Specifically if one function did something like calculating numbers and then the other function was used to display the result of those calculations.– DaruniaMay 24, 2020 at 5:59
-
My instructor said that you cannot use a function within a function and I am not able to contact her on Monday since it's a holiday. I just need an example really just any example of two functions being used together. Also sorry if I'm asking for "answers" it's just that I am not able to contact my instructor on Monday hence why I am asking here. I can't show off any code because I don't want to be "plagiarizing".– DaruniaMay 24, 2020 at 6:03
-
1Please demonstrate that you know how to use a single function. Otherwise the level of your understanding is unclear and answering in a way which matches your needs is next to impossible. Obviously you are not asking to have your homework done by somebody else, so this information is needed. If you are hoping for a homework help, then have a look here meta.stackoverflow.com/questions/334822/… the compromise described there is a good way to fix the lack of foucs on your specific programming problem.– YunnoschMay 24, 2020 at 6:38
-
1"I am not able to show off my code because it's from an older assignment" why does an older assignment keep you from providing the information which is needed to give you help on your current assignment? You could write a new little program which does nothing but use a single function. See minimal reproducible example.– YunnoschMay 24, 2020 at 6:42
1 Answer
The function in C++ is a subroutine which does it's own part of work. Function can return a value or return nothing (void), function can have arguments or not (void), in some programming languages the function without arguments is called procedures but in C++ all of them are just functions.
int main() - is a function from which started every C++ program (it's an entry point of user code). You can call a function from the other function and another function from that function... Even though, you can invoke the same function from itself (it's called recursion).
Here is an example of functions usage, there are six functions: four of them are doing simple math operations (+,-,*,/), fifth function invokes one of those functions if a char argument contains a valid operation. And the last function is main - the entry point of your program.
#include <iostream>
#include <string>
int addition(int a, int b)
{
return a + b;
}
int subtraction(int a, int b)
{
return a - b;
}
int multiplication(int a, int b)
{
int c = 0;
c = a * b;
return c;
}
int division (int a, int b)
{
return a / b;
}
int calculate(int a, int b, char operation)
{
if (operation == '+')
return addition(a, b);
if (operation == '-')
return subtraction(a, b);
if (operation == '*')
return multiplication(a, b);
if (operation == '/')
return division(a, b);
// not supported operation
return 0;
}
int main()
{
std::cout << calculate(3,2,'+') << std::endl;
std::cout << calculate(3,2,'-') << std::endl;
std::cout << calculate(3,2,'*') << std::endl;
std::cout << calculate(3,2,'/') << std::endl;
return 0;
}