help with function,,{just started using them}

this is my code

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 main()
{
	int a;
	a=larger(7,1200);
	cout<<a<<endl;





	return 0;
}

int larger(int x, int y)
{
	int max;
	if (x>y)
		max=x;
	else
		max=y;
	return max;
}


when i run the program it tells me that identifier larger is not found
Your function must be declared (prototyped) or implemented before it can be called. I recommend you declare the function before main.

closed account (3qX21hU5)
Like jlb said you need to have the function either defined or declared before you use it.

Like for example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int add(int a, int b); // Function declaration (IE Function Prototype)

int main()
{
    int one = 5;
    int two = 3;

    // Uses the function
    add(one, two);
}

// Function definition (IE where you define what the function will do
int add(int a, int b)
{
    return a + b;
}


That is the standard way of doing things usually. But this way works also

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// We can skip the declaration since we defined the object ABOVE where we are going to use it.
int add(int a, int b)
{
    return a + b;
}

int main()
{
    int one = 5;
    int two = 3;

    // Uses the function
    add(one, two);
}


This will work because it is defined before where we call the function in main().

This won't work though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    int one = 5;
    int two = 3;

    // Uses the function, won't work because C++ does not know what add() is because
   // It is defined AFTER main()
    add(one, two);
}

int add(int a, int b)
{
    return a + b;
}


Hope that helps a bit if you need to know anything else about functions just let us know and we would be glad to help you out.
closed account (3CXz8vqX)
In other words, what you have done is you've started making a cake and you don't have any of the ingredients in front of you. So you have to head off to the pantry, get the ingredient and start again. It works the same in programming. If you don't have the ingredients the program can't make a cake.

Think in the same way as a school science project.

1. Hypothesis
2. Apparatus
3. Method
4. Result.
5. Conclusion

1. Would be a comment describing your program.
2. Would be your functions, classes and the like.
3. Would be your main()
4. Compile time!
5. Did it work?

Contrary to Zereo the 'standard' is to declare functions at the top as procedurally this makes more sense. However in industry due to the scale of the programs (practicality) and often readability requirements, prototypes are more often used. Depending on the code, a prototype may also be required.
Contrary to Zereo the 'standard' is to declare functions at the top as procedurally this makes more sense. However in industry due to the scale of the programs (practicality) and often readability requirements, prototypes are more often used. Depending on the code, a prototype may also be required.


No, the "standard" way would be to place the declarations (prototypes) into an include file then include that file.

You should declare name larger before its using. For example

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
#include<iostream>

using namespace std;
int main()
{
	int larger( int, int );
	int a;
	a=larger(7,1200);
	cout<<a<<endl;





	return 0;
}

int larger(int x, int y)
{
	int max;
	if (x>y)
		max=x;
	else
		max=y;
	return max;
}
closed account (3qX21hU5)
Contrary to Zereo the 'standard' is to declare functions at the top as procedurally this makes more sense. However in industry due to the scale of the programs (practicality) and often readability requirements, prototypes are more often used. Depending on the code, a prototype may also be required.


That is just quite incorrect, like you said large and even small programs will have a large amount of functions. If you defined every function before main you would have a bunch of code at the top you don't want to look at right away and would have to scroll down to get where you want to be. This is why people use prototypes.

It is the standard way of doing things if you are going to have them in the same file. Depending on how many functions you have and if you are using classes like jlb said it might be even better to use separate files for the declarations and definitions.

I fail to see why having the definitions of classes, functions and everything else before main would be procedurally better. If anything it makes the code less readable and harder to follow.

Last edited on
closed account (3CXz8vqX)
*glances to L B* Yes but if you use an IDE...say Code::Blocks, you can minimise all of those functions.

j lb is better on this point, the standard way when using prototypes would be to use header files, but if it's all in the same file, nine times out of ten they'd be better at the top.

If it's a large program (note everything past the word 'However') is in complete agreement with you. Just because lots of people do it one way doesn't mean it's the correct or most efficient way to do it. There's often no point in a small file to declare a prototype because it will hinder readability instead of improve it. =P . Especially if you have no plans to extend the program...in which case, you should have used the header file method instead. There's only one exception to that rule as far as I am aware and that's where function needs to be aware of something before it has actually been set up.
closed account (3qX21hU5)
*glances to L B*

Huh?

I guess we could label it as a style preference which people have different ways they like it.

There's often no point in a small file to declare a prototype because it will hinder readability instead of improve it.


I don't see how it would hinder readability personally.

Just because lots of people do it one way doesn't mean it's the correct or most efficient way to do it.

Actually most of the time that is the case, though there is exceptions like in everything.

There is also more reasons why you should use prototypes for functions. Like so that the compiler can check whether you are passing the right arguments and resolve overloading.

It also helps with organizing functions that call other functions. If you declare prototypes at the top and define at the bottom you don't have to worry about what order the functions are defined if they use any of the other functions.

There is reasons why there is prototypes and generally even if it is a small program (I'm not talking about "Hello world" programs) it will help you maintain and avoid errors.
Last edited on
Topic archived. No new replies allowed.