JS基础教程——JavaScript基础知识(1.5函数-1.5.2函数的参数及返回值)

2013-12-27 liyangweb.com 李杨 JavaScript

参数即参变量,他是函数间相互传递信息的枢纽。如果经常进行编程,应该很容易理解将一个值当作参数传递给另一个参数,或者将一个函数的返回值当作参数传递给另一个参数。而用到这些数据的函数也是通过参数获取数值的。一个函数中的参数可以有多个,每个参数间以逗号进行间隔。具体带有参数的函数格式如下:

函数名(参数,参数2,参数三,......)


下面的实例充分显示出带有参数的函数应用方法。

function say(url){
    document.write(url);   //输出url的值
}
var str = "www.liyangweb.com";    //定义变量str
say(str);    //调用函数say,将str传递给函数


运行程序,会在屏幕上输出www.liyangweb.com,str变量的值以参数的形式传递给say函数,通过调用say函数实现str值的输出。

函数是一组语句的集合,在函数运行时总是各条语句依次执行,在一些情况下,语句可能包含return语句。在执行一个函数的时候,碰到return语句,函数立即停止执行,并且返回到调用它的程序。而return中带的值,则会返回到一个事先定义好的变量中存储。如果函数没有返回值,或者程序不需要返回值,则无需添加return语句。有返回值的和你熟语法格式如下:

函数名(){
    语句1;
    语句2;
    ....
    return ...;
}


定义两个函数的相加运算,返回相加后的结构值,最后通过一个变量接收并返回值,代码如下:

function number(x,y){
    var c = x+y;    //定义变量c,接收x+y的值
    return c;    //函数值返回
}
var c = number(5,8);    //用c接收函数number的值
document.write(c);      //函数结果输出


运行程序,会输出13。

由此空间,函数number由两个参数组成,并且分别进行传递。在函数中运算之后将结果用return关键字返回给变量,输出变量的值恰好为函数内运算的结果。

标签: JavaScript JS基础教程

评论:

thecoedge
2019-02-12 12:30
<a href="http://www.atlantaaiport.com/nike-free-run-5.0-gris-femmes-quilt-schuhef">nike free run 5.0 gris femmes quilt</a> <a href="http://www.bdimpact.com/kvinders-nike-air-max-bw-gr%C3%A5-guld-scarpef">kvinders nike air max bw gr氓 guld</a> <a href="http://www.dutchpuffers.com/air-jordan-5-wei%C3%9F-cem%C3%A4nnert-2017-year-kurpese">air jordan 5 wei脽 cem盲nnert 2017 year</a> <a href="http://www.klgateslaw.com/nike-shox-deliver-rose-pants-schuhef">nike shox deliver rose pants</a> <a href="http://www.nomorbogor.com/free-run-5.0-orange-schwarz-hair-schuheb">free run 5.0 orange schwarz hair</a> <a href="http://www.projabbag.com/salomon-xt-wings-3-jaune-or-chaussuresd">salomon xt wings 3 jaune or</a>
thecoedge http://www.thecoedge.com/
mylinkstv
2019-02-04 12:00
<a href="http://www.daddeaze.com/adidas-superstar-sneakers-footlocker-skoa">adidas superstar sneakers footlocker</a> <a href="http://www.debracousins.com/air-jordan-retro-4-kids-orange-white-sneakere">air jordan retro 4 kids orange white</a> <a href="http://www.yankaripark.com/nike-air-max-cage-gamma-blau-kurpesa">nike air max cage gamma blau</a> <a href="http://www.yicailottery.com/womens-nike-free-run-3-white-gold-sneakerb">womens nike free run 3 white gold</a> <a href="http://www.calebcraw.com/billig-nike-flyknit-lunar-3-kvinners-gr%C3%A5-shoesg">billig nike flyknit lunar 3 kvinners gr氓</a> <a href="http://www.liquidputter.com/green-bay-packers-acme-hat-hats">green bay packers acme hat</a>
mylinkstv http://www.mylinkstv.com/
fishonwheels
2019-01-31 14:05
<a href="http://www.mkdeltalandsgroup.net/nike-free-flyknit-3.0-electric-gr%C3%BCn-kaufen-kurpesd">nike free flyknit 3.0 electric gr眉n kaufen</a> <a href="http://www.mlarchitects.net/air-jordan-10-gs-rose-noir-kurpesc">air jordan 10 gs rose noir</a> <a href="http://www.mobilesoccerclub.net/nike-sb-lunar-gato-scarpa-new-slate-scarpec">nike sb lunar gato scarpa new slate</a> <a href="http://www.moonandcompany.net/lebron-12-low-alle-hvid-cem%C3%A6ndt-schuhee">lebron 12 low alle hvid cem忙ndt</a> <a href="http://www.mosesbodyshop.net/nike-zoom-todas-out-low-rosado-obuvf">nike zoom todas out low rosado</a> <a href="http://www.mrmpageantgowns.net/adidas-harden-vol-1-nero-bianca-rosso-2-butya">adidas harden vol 1 nero bianca rosso 2</a>
fishonwheels http://www.fishonwheels.net/
nongmobacon
2019-01-26 08:08
<a href="http://www.mikespothole.com/nike-zoom-winflo-2-lilla-svart-kurpesc">nike zoom winflo 2 lilla svart</a> <a href="http://www.theclaydoctor.com/ray-ban-sunglasses-lebanon-prices-philippines-sunglassesc">ray ban sunglasses lebanon prices philippines</a> <a href="http://www.allthatspool.com/padres-19-tony-gwynn-navy-blue-1998-turn-back-the-clock-stitched-mlb-jersey-nflr">padres 19 tony gwynn navy blue 1998 turn back the clock stitched mlb jersey</a> <a href="http://www.ihavewed.com/nike-air-max-1-essential-white-anthracite-wolf-grey-skoa">nike air max 1 essential white anthracite wolf grey</a> <a href="http://www.cloudsmartin.com/jordan-14-white-14-nba-sneakerf">jordan 14 white 14 nba</a> <a href="http://www.myachingoa.com/nike-free-og-midnight-fl%C3%A5den-kurpesa">nike free og midnight fl氓den</a>
nongmobacon http://www.nongmobacon.com/
culturalsq
2019-01-07 18:04
<a href="http://www.praiseti.com/jordan-2-sneakers-jordan-wade-runninge">jordan 2 sneakers jordan wade</a> <a href="http://www.cynshq.com/nike-air-max-97-dam%C3%A4nner-himmelblau-grau-schuhea">nike air max 97 dam盲nner himmelblau grau</a> <a href="http://www.fnjcompressor.com/asics-gel-lyte-5-rosado-slip-scarpeb">asics gel lyte 5 rosado slip</a> <a href="http://www.kifftradingco.com/nike-air-max-95-green-spain-shoeso">nike air max 95 green spain</a> <a href="http://www.phaqtory.com/nike-free-trainer-5.0-nero-mettuttiic-oro-bianca-kurpesb">nike free trainer 5.0 nero mettuttiic oro bianca</a> <a href="http://www.cialislaw.com/nike-air-force-1-high-decon-military-boot-black-zapatost">nike air force 1 high decon military boot black</a>
culturalsq http://www.culturalsq.com/
aboudincga
2018-12-30 15:18
<a href="http://www.arcadetogo.com/nike-huarache-gelb-and-schwarz-gelb-schuhee">nike huarache gelb and schwarz gelb</a> <a href="http://www.crowleyarms.com/jordan-11-hvid-ice-bl%C3%A5-obuvf">jordan 11 hvid ice bl氓</a> <a href="http://www.einsdigital.com/curry-2-scarpe-chaussuresa">curry 2 scarpe</a> <a href="http://www.sixseconddiet.com/nike-kd-7-orange-and-bl%C3%A5-ridge-butya">nike kd 7 orange and bl氓 ridge</a> <a href="http://www.tatteredhands.com/nike-zoom-clear-out-marrone-rosa-obuvc">nike zoom clear out marrone rosa</a> <a href="http://www.thetaxqb.com/jordan-11-armada-gum-high-scarpea">jordan 11 armada gum high</a>
[url=http://www.aboudincga.com/]aboudincga[/url]
myslinc
2018-12-28 03:36
<a href="http://www.lucasdow.com/herre-ralph-lauren-sweater-gul-bl%C3%A5-polow">herre ralph lauren sweater gul bl氓</a> <a href="http://www.juliemobley.com/kobe-9-masterpiece-on-vente-butye">kobe 9 masterpiece on vente</a> <a href="http://www.dgrlcd.com/nero-and-blu-polo-ralph-lauren-shirt-poloz">nero and blu polo ralph lauren shirt</a>
myslinc http://www.myslinc.com/

发表评论:

Powered by emlog 冀ICP备13011830号-1