New to Functions

New to C++ I just need some help better understanding what I am doing wrong inside the int main() and functions
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
The user will input 3 numbers and the program will output the largest of the three.
*/

#include <iostream>
using namespace std;

void showProgInfo();
int getNum();
int findLargest();
void showResult(large);

int main()
{
 showProgInfo;
 // I have to delcare these variables right?
 int num1;
 int num2;
 int num3;
 int large;

 // Is this where Im showing where I will be using num1, num2, etc.. in a function?
 num1 = getNum();
 num2 = getNum();
 num3 = getNum();
 large = findlargest(num1, num2, num3);

 showResult(large);

 return 0;
}

//first function
void showProgInfo()
{
  cout << "This program will ask you to enter 3 numbers and find the largest. "<< endl;
}

//second function
int getNum()
{
 int num;

 cout << "Enter a number: ";
 cin >> num;

 return num;
}

//third function im pretty sure i used if/else horribly here
int findLargest (int i=1, i>3, i++)
{
  int large;

  if (num1 > num2)
    large = num1;
  else
    large = num2;
  if (num3 > large)
    num3 = large;
  else
    large;

  return large;
}

//fourth function
void showResult (large)
{
  cout << "The largest number between " << num1 << ", " << num2<< ", " << "and " << num3 << " is " << large;
}
Last edited on
What does your compiler say?

Perhaps:
11:22: error: variable or field 'showResult' declared void
11:17: error: 'large' was not declared in this scope

 In function 'int main()':
15:14: warning: statement is a reference, not call, to function 'showProgInfo' [-Waddress]
15:14: warning: statement has no effect [-Wunused-value]
26:38: error: 'findlargest' was not declared in this scope
28:18: error: 'showResult' was not declared in this scope

 In function 'void showProgInfo()':
36:42: error: 'num1' was not declared in this scope
36:57: error: 'num2' was not declared in this scope
36:83: error: 'num3' was not declared in this scope
36:101: error: 'large' was not declared in this scope

 At global scope:
51:27: error: 'i' is not a type
51:28: error: expected ',' or '...' before '>' token

 In function 'int findLargest(int, int)':
51:5: error: default argument missing for parameter 2 of 'int findLargest(int, int)'
55:7: error: 'num1' was not declared in this scope
55:14: error: 'num2' was not declared in this scope
59:7: error: 'num3' was not declared in this scope
62:10: warning: statement has no effect [-Wunused-value]

 At global scope:
51:24: warning: unused parameter 'i' [-Wunused-parameter]
68:23: error: variable or field 'showResult' declared void
68:18: error: 'large' was not declared in this scope

Always start from first error(s). Fixing them can affect further code.

Line 11. What is "large"? If this is a function declaration and "large" is a name of function argument, then there should be a type of the argument too.

Line 15. showProgInfo; is not a function call; parentheses are missing: showProgInfo();

Line 26 could have a function call findlargest(num1, num2, num3) but you have not declared such function. You have declared a function on line 10, but that takes no arguments. Line 26's call needs a function that takes three arguments.

...
Whew, okay. Let's break this down.
First, your function prototypes need the variable's type you're passing to it. I usually just copy and past the function "header".
1
2
3
4
5
6
7
8
int findLargest();
void showResult(large);
// Needs variable type like so:
int findLargest(int num1, int num2, int num3);
void showResult(int large);
// Or even this works:
int findLargest(int, int, int);
void showResult(int);


Second, if you are going to use a variable (int num1..etc) in a function, you need to pass it to that function. The function can't "see" the variable unless you show the variable to it. This is done in the parameters like so:
1
2
3
4
5
6
7
8
// Calling the function:
showProgInfo(num1, num2, num3, large);
// Inside the () is sending those variable's value to the function
/*-----------------------------------------------------------------*/
// Function "header":
void showProgInfo(int num1, int num2, int num3, int large) {
// Inside the () is where you tell the function what type of values you sent them
// and declares them for your function. 


Third, like above the () after the function name are for parameters, not arguments.
1
2
3
4
5
6
int findLargest (int i=1, i>3, i++)
// Here you are trying to put a For loop argument in the parameters of the function.
// The compiler is looking for variables it needs to perform the function.
// This is what you're looking for:
int findLargest (int num1, int num2, int num3)
{


Fourth, your For loop (once you get it out of the function parameter) will never initialize because you are telling it to start counting when int i is greater than 3, but you have it equal to 1. Your For loop argument is saying i is 1, when i is greater than 3 start counting, add 1 to i every time you count. Also, the 3 different For loop arguments need to be separated by a ";" and not a ",". Your program doesn't need a for loop in it, but just for future reference:
1
2
3
for (int i=1, i>3, i++)
// Needs to be:
for (int i = 1; i < 3; i++)


Fifth, a better way to do the if/else statements would be using the &&(and) ||(or) logic operators. I don't know if you've learned them yet, but they are pretty easy to learn, and very useful. http://www.cplusplus.com/doc/tutorial/operators/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int findLargest (int num1, int num2, int num3)
{
    int large;
    if (num1 > num2 && num1 > num3) 
    { 
        large = num1;
    } else if (num2 > num1 && num2 > num3) 
    { 
        large = num2;
    } else {
        large = num3;
    }
return large;
}


Finally, clean up those typos. If you call a function and misspell the function name, the compiler won't know what you're trying to call. I'd recommend using a good compiler that can catch those typos and other syntax problems. I personally use CLion, but Visual Studios and others are just as good.

I am also a beginner, and with only about 3 months of C++ under my belt. So if anyone who has more experience can verify or correct anything I've written, I'm open to some constructive criticism. Here are some study aids for some of the problems you're having:
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/namespaces/

Practice is key jaybee. Let me know if you have any more problems.
Last edited on
Thank you so much for that great explanation Toffer. It has been a struggle trying to understand functions. Thanks for the advice about a good compiler. I'm downloading Visual Studios right now to help me out. I was trying to clean it up a bit and I keep getting errors largestFunc.cpp:31:13: error: expected primary-expression before ‘int’
largestFunc.cpp:31:23: error: expected primary-expression before ‘int’
largestFunc.cpp:31:33: error: expected primary-expression before ‘int’
largestFunc.cpp:31:43: error: expected primary-expression before ‘int’

no clue what to do here

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
/*
The user will input 3 numbers and the program will output the largest of the three.
*/

#include <iostream>
using namespace std;

void showProgInfo();
int getNum();
int findLargest (int num1, int num2, int num3,int large);
int large;
void showResult (int num1,int num2, int num3, int large);



int main()
{
  showProgInfo();

 int num1;
 int num2;
 int num3;



 num1 = getNum();
 num2 = getNum();
 num3 = getNum();


 showResult(int num1, int num2, int num3, int large);

 return 0;
}

So when you call a function, it's slightly different than writing a function, or a function prototype. When you call showResult on line 31, it doesn't need the int:

1
2
3
showResult(int num1, int num2, int num3, int large);
// like this:
showResult(num1, num2, num3, large);


The compiler thinks you are trying to declare those ints inside the function call. Since you already declared them above the function call, all you need is the variable name.

*Edited my second point above to show this
Last edited on
Functions can be a little tricky at first. I was on here about a month ago asking for help with them. They become second nature with enough practice though.
Topic archived. No new replies allowed.