C + + Learning (iv) Getting Started-function call

Source: Internet
Author: User
Tags function definition function prototype

Program Listing 2.5Ourfunc.cpp//Ourfunc.cpp--Defining your own function#include <iostream>voidSimonint);intMain () {using namespacestd; Simon (3); cout<<"Pick an integer:"; intcount; CIN>>count; Cin.Get();    Simon (count); cout<<"done!"<<Endl; Cin.Get(); return 0;}voidSimonintN) {    using namespacestd; cout<<"Simon says touch your toes"<< N <<"Times ."<<Endl;}

1. function format

Type functionname (argumentlist) {statements}

function definition: First, there is a function head, and then the function body in curly braces

The function source code is behind main (), and C + + does not allow the function definition to be nested within another function definition, each defined as independent and equal

 

#include <iostream>using namespace std;void Simon (int);                Function prototype double taxes (double); int main () {... return 0;                          function #1}void Simon (int) {...                                     function #  #}double Taxes (double) {...                                     function # # #}

2. Function Head

Start void indicates that Simon has no return value

The method is called as follows:

Simon (3);

Instead of

Simple=simon (3);

Simon's function header indicates that it accepts an int parameter and does not return any values.

And look back, int main () This function head

Returns an integer value for the function that has a return value, applies the keyword return to provide the return value, and ends the function.

return 0;

The return value of main () is not returned to other parts of the program, but is returned to the operating system.

An exit value of 0 means that the program runs successfully.

Keywords in C + +: int, void, return, and double cannot be used as variable names or function names, but they can be used as part of a function.

Using the same name (cout) as the object name and variable name in your program will confuse the compiler. In a function that does not use the Cout object for output, cout can be treated as a variable name, but cout cannot be used as the object name and variable name.

Define a function that has a return value:

Convert.cpp--Converts stone to pounds#include <iostream>int stonetolb (int);       Requires the existence of a function prototype, otherwise it will produce an error int main () {using namespace Std;int stone;cout << "Enter the Weight in stone:"; cin >> Stone; Cin.get (); int pounds = Stonetolb (stone) cout << stone << "stone=" << pounds << "pounds."; Cin.get ();} int Stonetolb (int sts) {return * STS;}

A function prototype describes a function interface, that is, how the function interacts with other parts of the program.

Why first write the function prototype in front?

say the image of a personification point. If the prototype is not written, the compiler reads the function in main, but because your function is written behind main . So at this point the compiler says you never heard of this function. So the compiler said wrong. There is no such function.

If the prototype was written in front. The compiler knows that you declared the function, and the compiler knows that he must be somewhere in your code, but not yet read it. So when you call this function, the compiler will read and read, and you will find the definition of your function in the back.

You can also put the function in theMainearlier, the compiler read the function definition at the beginning, and of course you have a function like that.,but supposeAIt's used in the function.b,bIt's used in the function.a. Maincalled in theaand theb, you even putABputMainThe front is useless. Because if youaplaced inbFront, here we go.a, you find that you want to invokeb, wrong ... It's also a mistake to change the order.

So it's best to first declare two functions in front, let the compiler go down and find

All of the function properties:

1. function head and function body

2. Accept a parameter

3. Returns a value

4. Need a prototype

Use using directives in multiple functions

Let the compilation instructions be placed outside the function, and in front of the two functions

The current concept is that it is better to only access a function that requires access to the namespace Std

There are four ways that programs access the STD for a namespace:

1. Place the using namespace STD, before the function definition, so that all functions in the file can use the namespace STD all elements

2. Place the using namespace STD in a specific function definition so that the function can use all the elements of the namespace Std

3. Use a similar using std::cout in a specific function, such as a compile instruction, rather than using namespace std; Let the function use the specified element

4. Do not use the instruction using at all, but when the element in the namespace STD is required, use the prefix std:: as shown below

std::cout<< "I ' m using cout and Endl from the Std namespace" <<std::endl;

C + + Learning (iv) Getting Started-function call

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.