C + + Learning (iii) Getting Started-functions

Source: Internet
Author: User

C + + functions are divided into two types: those with return values and those with no return values.

1. Functions with return values

Calling a function flow

, sqrt (6.25) is a function call, called function called called function, function called function called Call function.

The parameter is the information sent to the function, and the return value is the value sent back from the function.

Before using a function, the C + + compiler must know the parameter type and return value type of the function. If missing, the compiler will not know how to interpret the return value.

C + + programs should provide prototypes for each function used in the program

function prototypes for sqrt ():

Double sqrt (double);

If you omit the semicolon, then this line of code is interpreted as a function header, and the function body that defines the function is then provided.

There are two ways of providing a function prototype:

1. Enter the function prototype in the source code file

2. Contains the file Cmath, which defines the prototype (better)

Do not confuse function prototypes and function definitions. The prototype only describes the function interface (describes the information sent to the function and the information returned). The header file contains the prototype, and the library file contains the compiled code for the function.

Program Listing 2.4 Sqrt.cpp
Sqrt.cpp-Using the sqrt () function#include<iostream> #include <cmath>int main () { using namespace STD; Double area; cout << "Enter the floor area,in square feet,of your home:"; Cin >> Area; Cin.get (); Double side; Side = sqrt (area); cout << "That's the equivalent of a square" << side << "feet to the side." << Endl; cout << "How fascinating!" << Endl; Cin.get (); return 0;}

C + + library functions are stored in a library file, and if you run a program and get a message stating that _SQRT is an undefined external function, it is likely that the compiler cannot automatically search the math library (the compiler tends to add an underscore prefix to the function name-prompting them to have a final say in the program).

C + + allows you to declare a new variable anywhere in the program, or you can assign a value to it when you create a variable

Double side=sqrt (area) is called initialization

Cin knows how to convert the information in the input stream into a double type, and cout knows how to insert the double type into the output stream, which are intelligent.

2. Function variants

Some functions require multiple information

such as: POW ()

Double pow (double,double); The first argument is the bottom, the second argument is the power of the exponent

Answer=pow (5.0,8.0)

Some other functions do not accept any parameters

int rand (void); The function does not accept any arguments and returns a random integer

Omitting void, with empty parentheses, is an implicit declaration that does not accept any arguments

such as: Myguess=rand ();

In C + +, parentheses must be included in a function call, even if there are no arguments

There are also functions that do not require a return value, such as displaying a timetable and displaying it on the screen.

void bucks (double);

Because it does not return a value, it cannot be placed in an assignment statement or in another expression, but rather as a purely function call statement

Bucks (1234.56);

In some languages, there is a function called a return value, and there is no procedure called

The standard C library provides more than 140 predefined functions that are called if their functions meet the requirements.

Not satisfied, write on their own.

For library functions, the prototype is usually placed before the main () definition, and the source code of the new function is provided, followed by main ().

C + + Learning (iii) Getting Started-functions

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.