Explain this about the user made function in c++ ?
-
So the below is from an ebook i am learning c++ from , but i dont get the part below , its about user made functions , but what does the function SIMON does ? Why is it used when it does nothing ? "Every C++ program must have a main() function, which the user must define. Suppose you want to add a second user-defined function. Just as with a library function, you can call a user-defined function by using its name. And, as with a library function, you must provide a function prototype before using the function, which you typically do by placing the prototype above the main() definition. The new element is that you also must provide the source code for the new function. The simplest way is to place the code in the same file, after the code for main(). Listing 2.5 illustrates these elements. LISTING 2.5 ourfunc.cpp // ourfunc.cpp -- defining your own function #include <iostream> void simon(int); // function prototype for simon() int main() { using namespace std; simon(3); // call the simon() function cout << “Pick an integer: “; int count; cin >> count; simon(count); // call it again cout << “Done!” << endl; return 0; } void simon(int n) // define the simon() function { using namespace std; cout << “Simon says touch your toes “ << n << “ times.” << endl; } // void functions don’t need return statements The main() function calls the simon() function twice, once with an argument of 3 and once with a variable argument count. In between, the user enters an integer that’s used to set the value of count. The example doesn’t use a newline character in the cout prompting message. This results in the user input appearing on the same line as the prompt. Here is a sample run of the program in Listing 2.5: Simon says touch your toes 3 times. Pick an integer: 512 Simon says touch your toes 512 times. Done!"
-
Answer:
Let's start with the function prototype: void simon(int); // function prototype for simon() You need to stick this at the top of your code so that you can use the function in main(). The actual definition of the function comes after main(), and the compiler will get confused if you try and call a function it knows nothing about. So, this line just lets the compiler know that there is a function named simon that takes in an integer value and returns nothing. And now let's look at the actual function: void simon(int n) // define the simon() function { using namespace std; cout << “Simon says touch your toes “ << n << “ times.” << endl; } // void functions don’t need return statements The first line of code let the compiler know that at some point there was going to be a function named simon. Here we have the actual function and what it does. The keyword void tells the compiler that the function doesn't return any value. This make sense because the function just prints some text. It seems you are confused by having int n instead of just int. In the function prototype, we didn't need to give the integer argument a name. The function prototype just needs to state that the function needs to get passed an integer, so the compiler can make sure we use the function correctly. When we actually define the function, we need to give the parameter a name because it gets used in the body of the function. This statement in main() actually makes use of simon(): simon(3); // call the simon() function The 3 is the integer that is being passed into the function as an argument. In the body of the function, every place you see an n there is now a 3, and then the function does its thing. It's just like a function in math.
Soulja Buoy at Yahoo! Answers Visit the source
Other answers
What do you mean it does nothing? You pass an integer to it, and it displays the string "Simon says touch your toes x times.", where x is the integer you passed it.
oops
Related Q & A:
- How can I make Private Messages in Drupal 7 with user user pictures like in Facebook?Best solution by Drupal Answers
- How to access a nested function from another nested function in javascript?Best solution by devarticles.com
- How to check on all the permissions granted to a user and apply the same permissions to another user?Best solution by Stack Overflow
- How to implement C callback function in Swift?Best solution by pr8x.com
- How to learn C# (moving from C++ to C#?Best solution by stackoverflow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.