Java: Java Programming For Beginners a simple Start To Java Programming (Written By a software Engineer)
Download 0.82 Mb. Pdf ko'rish
|
Java Programming For Beginners - A Simple Start to Java Programming ( PDFDrive )
Environment
A complete set of variables and the values they contain form what is called the environment. Therefore, whenever you load a new webpage in the browser, you are creating a new environment. If you take the example of Windows 8, it creates an environment when an application starts and the same is destroyed when the application ends. Functions A set of statements that solve a purpose are referred to as a function. The purpose of using functions is code reuse. If your program uses functionality multiple times in the program, then it is implemented as a function, which can be called as and when required. Since, a function is to be called from within the code, parameters can be sent to the function from the code. Upon execution, the function returns a value to the calling function. The syntax for function declaration and definition is as follows: function multiply(a, b){ return a*b; } The name of the function must always be preceded with the keyword function. The variables a and b are parameters passed into the function and the function return the value obtained by computing a*b. This is a simple function, but you can implement complex and large function depending upon the functionality desired. Now that you have implemented the function, you must be wondering as to how the function is called. Here is an example: var x=2; var y=5 var c=multiply(x, y); Here, x and y are arguments that the function multiply will receive as parameters. JavaScript is a loosely typed language. What that means is that if you pass more arguments to a function than what it is expecting, the system simply uses the first n arguments required and discards the rest. The advantage of this functionality is that you can use already implemented functions and pass the extra argument to scale the function and add functionality to it. On the other hand, you will not be able to get any indication of error if you unintentionally pass the wrong number of arguments. JavaScript also provides some built-in functions for interacting with the user. These functions are as follows: alert This function raises an alert with a message and the system resumes operation after the user clicks on the OK button. Sample implementation: alert(‘Alert message!’); prompt This function presents a textbox to the user and asks him or her to give input. You can supply the default value in the presented textbox and the user can click on the OK button to accept that the value entered is correct. Sample implementation: var result = prompt(‘Enter a value’, ‘default value’); confirm This message gives the user the choice to OK or CANCEL an action. Sample implementation: var result = confirm(‘Do you wish to proceed?’); Function Scope Each variable that you declare possesses a scope of operation, which is the function within which the variable has been declared. This is called the local scope. Unlike, many other languages, which define local scope by the curly braces within which the variable lies, JavaScript’s local scope is same as function scope. In addition to this, JavaScript also supports the concept of global scope, in which variables can be declared global and thus, can be used anywhere in the program. Nesting Functions Functions can be nested at any level. In other words, a function can be called from within another function, which could have been called from a different function. However, it is important to note that the scope of variable is within the function in which they are declared. Download 0.82 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling