Functions: Can someone help me with the whole concept.

Hi everybody!

I am in my 7th week of C++. The professor goes so fast that my head is spinning. I have no idea what the concept of a Function is. I'm going to copy and paste his program below. When you compile it, the answer comes out to 5
5. Will somebody be so kind as to explain this to me (as if I were in kindergarten), and tell me how he got that answer? Thank you in advance!

Joanie

/*---------------------------------------------------------*/
/* */
/* Calling a user-written function */
/* */
/* Returning a value */
/* */
/*---------------------------------------------------------*/

/*---------------------------------------------------------*/
/* */
/* #includes */
/* */
/*---------------------------------------------------------*/

#include <iostream>
using namespace std;

/*---------------------------------------------------------*/
/* */
/* Main module */
/* */
/*---------------------------------------------------------*/

main ()
{

/*---------------------------------------------------------*/
/* */
/* Define storage */
/* */
/*---------------------------------------------------------*/

int Place = 3;

int Result;

/*---------------------------------------------------------*/
/* */
/* Function prototype */
/* */
/*---------------------------------------------------------*/

int Mystery (int);

/*---------------------------------------------------------*/
/* */
/* Call the function */
/* */
/*---------------------------------------------------------*/

Result = Mystery (Place);

cout << Result
<< '\n';

/*---------------------------------------------------------*/
/* */
/* End */
/* */
/*---------------------------------------------------------*/

}

/*---------------------------------------------------------*/
/* */
/* Mystery */
/* */
/*---------------------------------------------------------*/

int Mystery (int Location)

{

Location += 2;

cout << Location
<< '\n';

return Location;

}

I have no idea what the concept of a Function is.

The same as in maths, more or less. You put one or several values in (the parameters) and a value comes out (example: sin). f(x)=x+2 looks familiar? Yeah, that's your mystery function. And so, f(3)=5.
One difference is that functions can have side effects in most programming languages (such as printing a value to the screen).
A shorter version of the program:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int f(int x)
{
  return x+2;
}

int main()
{
  cout << f(3) << endl;
}


Don't let yourself be confused. Professors often try to weed out new students by making things seem more complicated than they are (or often enough, they just can't explain well). Programming is a very simple and logical thing, similar to maths.
I still don't understand. Yes, the f(x) = x+2 looks familiar from Algebra, but why was the answer two 5's? I'm really not an idiot. I just cannot seem to grasp the concept of all this, and I'm having a hard time finding someone who can make me understand. :(
A function is typically a piece of code that you will be calling multiple times in your program

a function setup is pretty simple, you tell he compiler what kind of value it will be returning, (i.e., int, string, void, ect.) then the function name. then inside brackets you define what the function will be doing when you call it. A return value is last in the code normally if you want your function to return a value.

Then you simply call your function in main() and it will do whatever you want designed it to.

for example:
1
2
3
4
void printString
{
	cout << "This is a string" << endl;
}


when you call printString() in you main function, you can probably guess what it will do.

Another example would be:
1
2
3
4
5
int mathEquation
{
	int number = 5 + 6;
	cout << number << endl;
}


Hope that helps you out.
Functions are a different code segment that gets "jumped to". Here's a simplistic example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void sayhi()
{   // when sayhi is "called", the program will run the code in this {code block}

  cout << "hi!" << endl;

  // when this code block exits, the program "returns" to whatever area of the program
  //  called the function... as if it never happened.
}

int main()
{
  // so we call the function once
  sayhi();  // this will execute sayhi's body.  Which prints "hi!" to the screen

  // once sayhi exits, the program continues here, immediately after the above function call.

  // we can even call it again if we want
  sayhi();  // (prints another "hi!" to the screen)
}


The output of this program would be
hi!
hi!


Your example is a little more complicated because it's doing 2 extra things:
1) It's passing a parameter to the function (input)
2) It's giving a return value (output)

Parameters let your function act on data "passed to" it.
Return values let your function "return" data out (pass data back to whatever called it).

Here's a simple function that sums two numbers together:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Return an int
int sum(int a, int b)  // take 2 int parameters
{
  return a+b;  //return the sum of those parameters
}

int main()
{
  int foo = 1;
  int bar = 2;

  // now let's call the sum function to sum foo and bar
  int baz = sum(foo,bar);

  // when we say =sum(...  like that, we are taking the function's return value and assigning
  //  it to our baz variable.

  // as a result...
  cout << baz;  // this prints "3"
}


Simple enough, right? Now here's what your original example is doing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int Mystery (int Location)  // take one parameter
{
  Location += 2;  // add 2 to that parameter

  cout << Location  // print it <HERE>
  << '\n';

  return Location; // and return it
}

int main()
{
  int Place = 3;
  int Result;

  // call the 'Mystery' function with 'Place' as the parameter
  //  assign the output to 'Result'
  //
  Result = Mystery (Place);

  // here, result is 5 (the output of the Mystery function)

  cout << Result;  // so this will print "5"
}


So why is the 5 printing twice? Because the code is printing it twice!

Remember that the Mystery function also outputs the value (the line marked <HERE> in the above code). It's being printed again in main, after Mystery is done. Hence, two prints.


EDIT: doh I'm too slow.
Last edited on
I want to thank all of you for your replies. I'm reading and digesting all of them. It seems to be sinking in a little. I'm going to keep going over and over them until I get it. I need an 'A' in this class! :)

I'm sure I'll be back!

Joanie
Okay, this is for Disch....

I know my prof named his function Mystery, but where did "Location" come from? Then, when it says "return Location", where does it return it to? Back to the function?
Another thing....the way I'm understanding it, and correct my twisted mind if I'm wrong, but is a function kind of like writing a macro? So after I write my Mystery function, do I save it somewhere, and then use it again in another program if I wanted to? Lord, help me.
Disch has pretty much covered everything I was going to say, so instead I'm just going to post this and tell you to read it and then go back and try to figure out your prof's code.

http://www.cplusplus.com/doc/tutorial/functions/

It helped me understand it when I was starting, I'm pretty sure you should be able to follow it. Let me know if you still don't understand what's going on.
but where did "Location" come from?


That's the parameter (the input):

1
2
3
int Mystery (int Location)  // <-  in parenthesis here is the parameter
{
//... 


When you "call" Mystery, you need to give a value to it. This value gets assigned to 'Location':

1
2
3
4
5
6
7
int main()
{
  Mystery(5);  // call Mystery with Location=5

  int foo = 6;
  Mystery(foo);  // call Mystery with Location=foo .. which is 6
}


Then, when it says "return Location", where does it return it to?


It returns to whatever called it, which in this case, is main.

main calls Mystery
Mystery returns to main.

But it doesn't return to the start of main, it returns to the line of code which called it.

1
2
3
4
5
6
7
8
int main()
{
  Mystery(5);  // call Mystery here
  //  <- Mystery returns to here

  // if we call Mystery again...
  Mystery(10);
  //  <-  this time it returns to here 



but is a function kind of like writing a macro?


Kinda sorta. Macros are like automated copy/paste operations. A function is a separate block of code. Both can be used for the same thing, but for most things functions are better and more flexible.

On the other hand macros can do some things functions cannot, so each have their uses.

Put macros out of your mind for now, though. You shouldn't need them until you get more familiar with the language.

So after I write my Mystery function, do I save it somewhere


You save it in a cpp file. It's just more code. Just like your main(). It's just more code for your program to run.

and then use it again in another program if I wanted to?


Yes you could do that.
Thank you so much for all of your help, Disch, your examples have been invaluable. Also, great link from TheMeerkat. I appreciate all the time you take to help us newbies. :)

Joanie
Topic archived. No new replies allowed.