Inserting User Defined Functions

hey there, pretty simple problem, im trying to write a simple program using 3 functions with 2 user defined functions in it. Ive been running into the error code uninitialized local variable C4700 for both "blind" and "run", i was just wondering, to me it seems ive given the program the prototype for both functions, defined both, and was just wondering how i can insert it into the main function, heres a copy of the source........

//program using user defined functions "three blind mice"

#include <iostream>
int blind(int blind);//prototype for blind
int run(int run);//prototype for run

int main()
{
using namespace std;
int blind;
cout<< blind << endl;
cout<< blind << endl;
int run;
cout<< run << endl;
cout<< run << endl;

return 0;
}

int blind(int blind)//definition for blind
{
using namespace std;
cout<< "Three blind mice";

return 0;
}

int run(int run)//definition for run
{
using namespace std;
cout<< "See how they run";

return 0;
}

........ if ya could please include what/how/why of my errors and how i could rectify this in the future, again guys, the helps greatly appreciated, and thank you for sticking with my simple jack @$$, lol
This isn't how you use functions in C++. What you seem to want is something like this:

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
#include <iostream>
int blind();//prototype for blind /* I removed the arguments to both of these functions, you don't use them */
int run();//prototype for run

int main()
{
using namespace std;
/* int blind; Commented out, effectivley deleted from your code */
cout<< blind() << endl; /*This is how you pipe a function to std::cout with no arguments in C++ */
cout<< blind() << endl; /*And Here*/
/* int run; Commented out, effectivley deleted from your code */
cout<< run() << endl; /*And Here*/
cout<< run() << endl; /*And Here*/

return 0;
}

int blind()//definition for blind /* Arguments also removed from definitions */
{
using namespace std;
cout<< "Three blind mice";

return 0;
}

int run()//definition for run
{
using namespace std;
cout<< "See how they run";

return 0;
}


EDIT: I deleted the local variables from your code because they aren't used. You can technically name a variable the same as a function but if you do this and publish your code for other people to use, I will find you, sit you down and have a little "talk".

Evidently you cannot do this and use both in the same function, well it's better for everyone that way anyway.
Last edited on
See my comments here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
int blind(int blind);  //  These are a OK prototypes.  Since you have prototypes,
int run(int run);  //   you can call the function in your code

int main()
{
  using namespace std;
//  int blind;   // this is creating a variable that has the same name as your function
    //  get rid of that, you don't need a variable for blind.

  //cout<< blind << endl;  // this is printing the contents of your variable 'blind'
  //  again, get rid of it.  You don't care about this variable.
  //   this has nothing to do with your function -- you just gave it the same name

// if you want to call your blind function, you need to give it parenthesis:

  blind(0);  // this will call the blind() function with '0' as the passed parameter 



Note that you don't do cout << blind(0); because blind() doesn't return anything meaningful that you want to print. blind() itself will output to cout. Basically you either put cout in blind() or in the code that calls blind(). You don't do both.

run() has the same problem. You're not calling the function, but instead are creating another variable with the same name and printing the contents of that variable (useless).



Also -- there's no point to have parameters here. Why does blind() need an int passed to it? Why does run()? Why are they returning ints?

I would change them like so:

1
2
3
4
5
6
7
8
9
10
void blind();  // make it return a void (no return value), and don't pass any int
void run();

//...

void blind()
{
  using namespace std;
  cout << "Three blind mice.";
}


Then when you want to call it:

1
2
3
int main()
{
  blind();  // this calls your blind funciton, which prints "Three blind mice." 



I also recommend you read the tutorial on functions on this site.


EDIT:

doh, too slow.
Last edited on
so basically none of my functions or prototypes need arguments due to the fact that they are just simple outputs and require no information other than what to display, gotchya, cool man, much appreciated(ps, normally "talks" dont go all that well with ex green berets, but no worries, i get where your going, lol), and again, thank you for taking the time to explain why, cant count how many times i just get lines of code back with no explanation as to what my mistake was and why this rectify's it..... kinda like teaching japanese without the english translation..... and yea, right now im not publishing anything, just started learning a week and a half ago and all this is just for educations sake, pretty much all of this wont ever make it off my laptop
Also you'll note that when you run this you'll have a '0' at the end of each line, this is because you declared to your compiler in lines 2 and 3 that run() and blind() were going to return an integer, the integer you returned at the end of each function definition was '0' and that was sent to std::cout on lines 9, 10, 12 and 13 in my repost of your code and displayed accordingly.

If you do not want this zero there you can:

- Declare the functions like this void blind(); and void run

- Declare the functions like this std::string blind(); and std::string run and define them like this:

1
2
std::string blind()
{return "Three blind mice";}

and
1
2
std::string run()
{return "See how they run";}
ive tried reading the tutorials section but ive been using C++ Primer 5th edition and its worked alot better for me thus far, i still use it as a reference but for the most part i just like the way the primers worded and set up, easier for me to understand i suppose......
so basically whenever creating a function or statement with no return value(as far as i understand) you can either use something like "()"(blank space, so the computer assumes theres no return value or argument), (0), or void "statement here", correct?
and again guys, i really cant express how much help this is, and i do greatly appreciate it, i dont start back to school until summer so to get a head start on things is great, plus its kind of a nuisance to try and use a book when theres no one there to explain certain questions i may have, so once again, thanks fellas
Simply, there's 3 basic parts to a function:

1) The return type (ie: the function output)
2) The name of the function
3) The parameters (ie: the function input)

Now, functions always have parenthesis after the name. That's what identifies them as functions. In the prototype and definition, the parameter list goes inside those parenthesis.

For example:

 
void func(int foo);


Here, 'void' is the return type (indicating the function does not return any output)
'func' is the function name
and 'int foo' is the parameter (indicating the function needs 1 int passed to it in order to work)


Let's say the function looks like this:

1
2
3
4
void func(int foo)
{
  std::cout << "foo = " << foo;
}


Now when you call the function, you need to give it some value for 'foo'. This is done by putting the number in the parenthesis when you call it:

1
2
3
4
5
int main()
{
  func(5);  // this passes '5' as 'foo' in the function
   // so it will print:  "foo = 5"
}


If the function takes no parameters, then it takes no input. For example:

1
2
3
4
void func()
{
  std::cout << "No input";
}


Now we don't need to give it any input when we call it, however we still need parenthesis to call it... so we call it like so:

1
2
3
4
int main()
{
  func();  // calls func(), which prints "No input"
}




Returning values / output is another topic that I might explain later if nobody else does.
Sorry if you read it already but don't have time to fulfill the last post I made :(
cool, thanks disch! That was actually ridiculously helpful, like i said the main problems i had with the tutorial on here and various other ones(aside form primer 5th ed) were the way they explained(or didnt), the amount of detail and the vernacular or SYNTAX(oooooh, c++ buzzword!) they used, i have no problem comprehending things, im just a firm believer that you have to find a way to relate the information to the peron, and thats key, thats why alot of people think they CANT learn things when its just theyre approaching it the wrong way, anyways, much obliged sir!
Returning values let's you get output from the function. For example let's say you want a function that applies the following formula:

c = 5*a - 3*b

where 'c' is the output and 'a' and 'b' are inputs. So you would pass a and b to the function, and the function would return c.

Here's an example:

1
2
3
4
5
6
// a function that returns an int
//  and takes 2 ints as params (a, b)
int func(int a,int b)
{
  return (5*a) - (3*b);  // return what we want calculated for 'c'
}


To call the function, we need to receive the returned value somehow. This is done by using the function call inside another statement --- the function call effectively gets "replaced" with what the function returns:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
  int c = func(3,2);  // here we assign the returned value to c

  std::cout << c;  // this will print "9" ... the output of our function

  // however you don't have to assign the output to a variable, you can use
  //  it in most situations where you can use a variable.  Like in a cout statement:

  cout << func(1,0);  // perfectly legal.  prints "5"

  //  ... or in a conditional statement

  if(func(1,1) == 2)
    cout << "it's true!";
  else
    cout << "it's false.";

  //  the above will print "it's true!" because func(1,1) will return 2
}
Topic archived. No new replies allowed.