I am new to programming and need some help with home work. I have to have two user defined function and a main function. One user defined function has to pull two integers from main, multiply them and put the product back in main. The second function has too pull the product from main and print it.
#include <iostream>
usingnamespace std ;
int firstFunkMultipy (int n, int k); // take (n) & (k) from main multiply and
// return to main
{
int n;
int k;
r=n*k;
return (r); //return (r) to main so it can be called
//for secondFunkPrint
}
int secondFunkPrint (int result); //Take (result) from main which is product of
//firstFunkMultiply
{
cout << "The product is!" << result;
}
int main()
{
int n, k, r; //Int as variable
int result
cout << "Enter n (positive integer) : " ;
cin >> firstFunkMultipy (n) ; //is this how i get input from main to go
//to firstFunkMultipy
cout << "Enter k (positive integer) : " ;
cin >> firstFunkMultipy (k) ;
result = r; //(r) is product of firstFunkMultipy and becomes result for call
//in secondFunkPrint
system("PAUSE");
return (0) ;
}
Once (n) and (k) have a value then main is looking for the value of (result) which tells it to go to my (firstFunkMultiply).
In the user defined function (firstFunkMultiply) I have two questions why doesn’t (r) have to integer, and (return r) that (r) is the product that you are returning to main correct?
So now back in main result = r which is the output of firstFunkMultiply correct?
Main doesn't see r. The way a function works is that it has an interface and an implementation, note this will be important when you get to classes. The interface is how the rest of the world sees the function. The rest of the world does not see the implementation. The interface between a function and the outside world is the function prototype. The code between the braces is the implementation. Take for example a person walking down the street. The "interface" would be his eyes, ears, nose, skin, mouth, and tongue. The "implementation" would be his arms, legs (for movement) and his brain to process information. But when you talk to this person you don't control the brain or arms or legs. An imperfect analogy.
1 2 3 4 5 6 7 8 9
int firstFunkMultipy (int n, int k); // take (n) & (k) from main multiply and
// return to main
{
int n;
int k;
r=n*k;
return (r); //return (r) to main so it can be called
//for secondFunkPrint
}
First, while function prototypes require semicolons, the function call line (not sure about proper name) does not. Second, the variables n and k, which are in the call line, are visible to the function, you do not need to redeclare them inside the braces.
Finally when you say:
firstFunkMultipy (k) ;
Since main does not know what you call your variables inside the function, you don't need to match the variable names. Also a function tries to execute it everytime you call it, not once you have passed it enough variables.
firedraco: My solution is compiled and tested (and it works).
I always learn best when people give me the correct code, then I can figure out what I did wrong myself and learn from my mistakes. I'm sure plenty of others will explain what he did wrong, but he needs to be able to see how to do it right.
Just to try and clarify a few little things for you...
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
usingnamespace std ;
int firstFunkMultipy (int n, int k); // take (n) & (k) from main multiply and
// return to main
{
int n;
int k;
r=n*k;
return (r); //return (r) to main so it can be called
//for secondFunkPrint
}
The reason that this is incorrect is because when using functions there are two things you can do: create a function prototype before main() and define it after, or define the function when you create its protoype (although at this point i'm not sure if it is actually classed as prototype?).
What you have done is mixed the two together in that you have created the prototype (finished it with a semicolon) and then tried to give it its definition with the curly braces.
This is how the two methods should look... protoype only:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
int firstFunkMultipy (int n, int k); // semicolon finished the prototype statement
int main()
{
... //
}
int firstFunkMultipy (int n, int k) // no semicolon so the following code is with this function
{
int n;
int k;
r=n*k;
return (r); //return (r) to main so it can be called
//for secondFunkPrint
}
proto with definition
1 2 3 4 5 6 7 8 9 10 11 12 13
int firstFunkMultipy (int n, int k) // no semicolon
{
int n;
int k;
r=n*k;
return (r); //return (r) to main so it can be called
//for secondFunkPrint
}
int main()
{
... //
}
Ok i've just ran out of time so I'll leave you with just this information on using functions, sorry!
I will add though that in you're function you didin't need to use r=n*k; return r;
You can just use return n*k
@Xeon: system("pause") is evil, and it also may not work on all systems.
But regardless, you still shouldn't just give out solutions. You need to help them understand what they did wrong (like you said), and then they will learn how to do it right.
Firedraco: I know system("pause") is evil, but I don't want to change any of the functionality of this code. It's perfectly fine and acceptable for his homework assignment.
Still, I guess we have a fundamental disagreement on learning methods. I'd rather be given the solution to a problem and work out myself what I did wrong than have someone explain it to me. But that's just me, so I respectfully understand your concern.
Of course, I AM glad that mcleano is explaining prototyping...that confused me for ages when I was first trying to learn c++ in my early days.
While showing him code might be a good idea, you should still point to code on another website, or towards a tutorial on the matter, if he is having issues understanding the basic idea, as he then begins to learn how to look for the information.
I am trying to build off my previous submission. This is an Averaging program where numbers are put into an array and the data call is a function. I get an error that identifier for function not found.