The use of the function of the path of Linux learning

Source: Internet
Author: User

What is a function?

function is the ability to encapsulate some code to implement code reuse.

Functions cannot be run independently, only function calls can be performed, and can be called multiple times

The benefits of using functions prevent code from being reused multiple times, and program code is more structured.


So how do we define the function?

There are two kinds of structures for defining functions

Structure One:

function FUNCNAME {

COMMAND

}

where function is the keyword, funcname is the function name, command is executed in the function body


Structure two:

FUNCNAME () {

COMMAND

}

Where FuncName is the name of the function


We said that when a function is called, the command in the body of the function takes effect and the function cannot run on its own.

So how do you call the function?

In fact, it is very simple to call directly with the function name

For example, use the PING function name to call the function

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/3E/E3/wKiom1PH9WLTAxt3AAF16tlNZGM414.jpg "title=" p1.png "alt=" Wkiom1ph9wltaxt3aaf16tlnzgm414.jpg "/>


Parameters of the function

A function can pass arguments, only the arguments are appended to the function.

Shaped like FUNCNAME argument1 argument2 ...

The parameters of a function in Linux correspond to a positional variable, which means that the position variable corresponding to each parameter is $1,$2,$3 ...

And so on, and so on.

It is therefore possible to invoke the value of a parameter in a function body only by using positional variables.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/3E/E3/wKiom1PH9n_Sf_3jAAFYG8gtmtA064.jpg "title=" p2.png "alt=" Wkiom1ph9n_sf_3jaafyg8gtmta064.jpg "/>


Before we learned the script, we knew we could use the exit command to customize the return value of the execution state of the script. Where 0 means the script executes successfully, and not 0 indicates that the script execution failed.

So in the function, how to define the function's execution state return value?

You can use the return command to

where 0 indicates successful function execution

1-255 indicates failure of function execution

Whenever the return keyword is encountered in a function, the system exits the function and no longer executes the contents of the function.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/3E/E3/wKiom1PH96_igf5-AAGWf_83ED8153.jpg "title=" p3.png "alt=" Wkiom1ph96_igf5-aagwf_83ed8153.jpg "/>

Where $? is used to hold the return value of the function execution state.

This article from the "Linux Learning Path" blog, declined reprint!

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.