[C + +] Fucntions

Source: Internet
Author: User
Tags function prototype

Statements

A Break statements terminate the nearest wile, does while, for or switch statement. A break affect only the nearest enclosing loop or switch.

As any block, variables declared inside a try block is inaccessible outsite the block-in particular, they is not acces Sible to the catch clauses.

Exception.

A try block Maigh call a function, contains a try which calls another funciton with their own try and so on. The search for a hander reverses the call chain. If no appropriate catch is found, execution are transfered to a library function name terminate. The terminate would garantee to stop further execution of th problem.

If there is no try blocks, they can be no handlers. If a program no try blocks and an exception occurs, then terminate are called and program is exited.

Programs that properly the "clean up" during execution handling is saied to being "exception safe".

Functions

Parameters and Arguments

Arguments is the initilizers for a function ' s parameters.

Local Objects

In C + +, names has scopes, and objects have lifetimes

    • The scope of a name is the "part of" of the program's text in which. s, the name is visible.
    • The lifetime of an object was the time during the program's execution that the object exists.

Parameters and variables defined inside a function body is refered to as local variables. They is "local" to that function and hide declaration of the same name made on outer scope.

Obejcts defined outside any functions exist through the program ' s execution. Such object is created then the program starts and is destroyed until the program ends

The lifetime of varibles depends on what it is defined.

Automatic object. Objects that exist if a block is execution be known as automatic Objects.

Local static object. Each local static object is initilized before the first time execution pass through the object ' s definition. Local static objects is not destroyed when a funciton ends; They is destroyed when the program terminated.

size_t count_calls () {    static0;    // value would persist across CALSS    ctr++;     return Ctr;} int Main () {    for0; i++) {        << count_calls () << Endl;    }}

This program would print the number from 1 through inclusive.

Function Declartions is also known as the function prototype.

Recall that variables is declared in Haeder files and defined in source file. Function should is declared in header files and defined in source file.

The header that declares a funciton should is included in, the source file tha defines that function.

Argument passing

Parameter initialization works the same as variable initialization.

If The parameter is a reference then the parameter was bound to its argument. Otherwise, the argument ' s value is copied.

When the Aprameter is a reference, we say the It correspondency argument is ' passed by reference ', or that function is ' call Ed by referenced ". As with any other reference, a reference paramter are an Alisa for the object to which it is bound.

When the argument value is copied. The PARAMETR and argument are independent object. We say such arguments is ' passed by value ', or the function is ' called by value '.

When we initiliaze a non-reference type variable or parameter, the value of the initializer is copied.

Pointers behave like any other non-reference type. When we copy a pointer, the value of the pointer is copied. After the copy, the pointers is distinct, but pointing to the same object.

void Reset (int *IP) {    0;    // Change the value of the object to which IP points    0;    // Change the local pointer, the argument is unchanged.}

Usage:

int  the ; Reset (&i);    // Change I, but not the address of I

Programmers accustomed to programming in C often use pointer paramters to access objects outside a function. In C + +, programmers generally use reference parameters instead.

Recall that operations on a reference is actully operations on the object to which the reference refers

void Reset (int &i) {    //  I was just another name for the object passed to reset
   0;    // Change the value of the objet to which I refers}

When we call this version of reset, pass the object directly, no need to pass its address.

int  the ; Reset (j);         // passed by reference, the value in J is changed

Reference parameter that is not changed inside a function should is references to Const.

Never return a reference or pointer to a local object.

When a function completes, it storage is free. After a function terminates, the references to local object refer to memory, which is no long valid.

//disaster:this function returns a reference to a local objectConst string&Mapip () {stringret; if(!Ret.empty ()) {        returnRet//Wrong:return A reference to a local object}Else{        return "Empty";//wrong: "Empty" is a local object    }}

References returns is Lvalues

Calls to function that return references is Lvalu. Other return types is rvalue.

 char  &get_val (string  &str , string  ::size_type ix) { return   Str[it];}  int   Main () { string  s ( " a value  "     ); Get_value (S,  0 ) =  " a  ; //     return  0  ;}  

Overload Functions

Functions that has the same name but different parameter lists and that appear in the same scope is overload.

Default Arguments

SZ WD = the;Chardef =' ';Charht ();stringScreen (SZ = HT (), Sz = WD,Char=def);voidF2 () {SZ WD= -;//hide the definition of WD, but is not a change the defaultdef ='*';//Change the defaultwindow = screen ();//Call Screen (HT (), +, ' * ')}

Reference:

C + + Primer, fifth Edition, Chapter 6 Functions

[C + +] Fucntions

Related Article

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.