Function Homework.

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.

Thanks for the help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream> 
using namespace 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) ;
}
Your functions have several problems...instead of telling you all of them, you can read this:
http://www.cplusplus.com/doc/tutorial/functions/

You should see the errors after you are done with that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream> 
using namespace std; 

int firstFunkMultiply (int n, int k){
	int r=n*k;
	return r;
}

void secondFunkPrint (int result){
	cout << "\n\nThe product is!" << result;
	return;
}

int main()
{ 
	int n, k, r, result;
	cout << "Enter n (positive integer) :  " ;
	cin >> n;  
	cout << "Enter k (positive integer) : " ;
	cin >> k;
	result = firstFunkMultiply(n,k);
	secondFunkPrint(result);
	system("PAUSE");   
	return 0;
}
XeonXT: Don't just give the solutions (despite that it has problems...)
Couple of questions please; sorry I am a little dense in understanding the data call and return. I have read thread http://www.cplusplus.com/forum/beginner/8318/

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?

Thanks for the help
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.
After reviewing it again I admit, r didn't need to be declared in main(). But that doesn't break any functionality...
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> 
using namespace 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.
But he also starts to learn bad habits.

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.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#include <iostream>
using namespace std;

int main() {
  int  number[10];
  int  count = 0;
  int  sum = 0;
  char reply;

  do {
    cout << endl;
    cout << "Enter the positive interger  in whole numbers: ";
    cin >> number[count];
    count++;
    cout << "Do you want to enter the next positive interger  (y/n)? ";
    cin >> reply;
  } while(count < 10 && tolower(reply) == 'y');

  if(count == 10) {
    cout << "maximum number of positive intergers reached." << endl;
  }

  double average = 0.0;

  for(int i = 0; i < count ; i++) {
    sum += number[i];
  }

  average = sum/count;
    cout << endl;
  printFunction(average);
  return 0;
}
void printFunction (int average)
{
	cout << "\n\nThe average is!" << average;
	return;
}
Figured it out, function's have to go before main other wise they don't get seen.
Topic archived. No new replies allowed.