I am having a hard time understanding in when functions do and don't need a parameter list. Recently, I had a programming assignment in which I had to create two different functions for two different cases. The first case was calculating exponents (I know, its built in but w/e). The function prototype was int Exponent(int, int). You entered the base and power. The second case was converting Fahrenheit to Celsius. It's prototype was double ConvertTemp(). You put in number of degrees.
Why does Exponents have a parameter list, but ConvertTemp does not?
I got it right but I, more or less, just did whatever the guy beside me did. Missed a few classes. Turned out to be important ones, huh?
This is what our return type will be (if no return then it should be void)
int
This is just the name of our function
Exponent
Parameters passed to the function (if any)
(int, int)
Some functions require parameters to pass to the function in order for it to do it's job and some don't. Just like some functions require a return and some are declared void, or no return type.
#include <iostream>
usingnamespace std;
int sum(int a, int b)
{
return a+b;
}
int something()
{
return 43;
}
int main()
{
int u = 6;
cout << sum(54,u) << endl << something();
}
There's a really simple example of parameters and no parameters :P
what does "parameters passed to it" mean?
in my example parameters passed to function sum are 54 and u
Why doesn't "ConvertTemp" have a parameter list?
u dont need parameters in some cases:
1 2
int celsius = 15;
double fahrenheit = celsius*ConvertTemp();
I don't really understand how my example wasn't clear, I mean this is the most basic stuff there is. Here is a program that sets two integers as 10 & 5 and another integer named Add, which initializes by calling MyFunc. You can see how it passes two parameters (10 & 5) to MyFunc() which then adds them and returns the value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int MyFunc(int oneArg, int twoArg);
int main()
{
int i = 10, j = 5;
int Add = MyFunc(10, 5);
return 0;
}
int MyFunc(int oneArg, int twoArg)
{
return (oneArg+twoArg);
}
See how MyFunc uses the parameters passed to it to add them together and return the result? Some functions don't require any parameters to be passed to it just like some have no return function.