Define Function expression in JavaScript

Function Expression

When we create a function and assign it to a variable, known as a function expression.

Note: –

  • You can‟t call function expression before the function definition.
  • Function expressions in JavaScript are not hoisted, unlike function declarations.

Anonymous Functions

Anonymous functions allow the creation of functions that have no specified name.

  • Can be stored in a Variable
  • Can be Returned in a Function
  • Can be pass in a Function

Store Anonymous Function in Variable

Passing Anonymous Function as Argument

Returning Anonymous Function

Tagged : / / /

How do I use a default parameter in JavaScript?

Default Parameter

Syntax: –
function function_name (para1, para2=“value”, para3) // problem undefined
{
Block of statement;
}
Syntax: –
function function_name (para1, para2=“value1”, para3=“value2”)
{
Block of statement;
}

JavaScript also allows the use of arrays and null as default values.

Rest Parameters

The rest parameter allows representing an indefinite number of arguments as an array.

Syntax: –
function function_name (…args)
{
Block of statement;
}


Syntax: –
function function_name (a, …args)
{
Block of statement;
}

The rest operator must be the last parameter to a function.

Rest Vs Arguments

There are three main differences between the rest parameters and the arguments object:-

  • Rest parameters are only the ones that haven’t been given a separate name, while the arguments object contains all arguments passed to the function.
  • The arguments object is not a real array, while Rest Parameters are Array instances, meaning methods like sort, map, forEach or pop can be applied on it directly.
  • The arguments object has additional functionality specific to itself (like the callee property).

Tagged : / / / /

How to Write Function in JavaScript?

Function

Function are subprograms which are used to compute a value or perform a task.

Type of Function

Library or Built-in functions

Ex: – valueOf( ) , write( ), alert( ) etc

– User-defined functions

Creating and Calling a Function

Creating a Function

Syntax: –

function function_name( )
{
Block of statement;
}

Calling a Function

Syntax: –
function_name( );

Ex: –
display( );

Rules of Function

  • Function name only starts with a letter, an underscore ( _ ).
  • Function name cannot start with a number.
  • Do not use reserved keywords. e.g. else, if etc.
  • Function names are case-sensitive in JavaScript.

How Function Call Works

The code inside a function isn‟t executed until that function is called.

Function with Parameters

Syntax: –
function function_name (parameter1, parameter2, ….)
{
Block of statement;
}

  • JavaScript function definitions do not specify data types for parameters.
  • JavaScript functions do not perform type checking on the passed arguments.
  • JavaScript functions do not check the number of arguments received.

Call Function with Parameter

Syntax: –
function function_name (para1, para2, ….)
{
Block of statement;
}
Syntax:-
function_name(argument1, argument2);

Function Argument Missing

If a function is called with missing arguments, the missing values are set to undefined.

Arguments Object

The arguments object contains an array of the arguments used when the function was called.
This object contains an entry for each argument passed to the function, the first entry’s index starting at 0. The argument’s object is not an Array. It is similar to an Array but does not have any Array properties except length.

Many Function Arguments

If a function is called with too many arguments, these arguments can be reached using the arguments object which is a built-in.

Tagged : / / /

Tutorials for PHP Function with Parameters or Arguments

What is Function with Parameters or Arguments?

The information or variables included within the function’s parentheses are referred to as parameters. These are used to keep track of values that can be used during runtime. The comma(,) operator allows the user to input as many arguments as he wants. These options are used to accept inputs during runtime. When transferring numbers, such as during a function call, arguments are used. A parameter is a container for parameters that are given to a function. A value supplied to a function is referred to as an argument. In layman’s terms, both parameter and statement have the same meaning. It’s crucial to keep in mind that each parameter must be accompanied with an argument.

PHP functions,Parameters and arguments

Syntex:-

function function_name($first_parameter, $second_parameter) {
    executable code;
}

Example:-

Setting Default Values for Function parameter:-

In PHP, we may define default arguments for function parameters. If no argument is supplied for a parameter with a default value, PHP will call the function with the parameter’s default specified value.

The default value for the parameter $num in the previous example is 12; if no value is supplied for this parameter in a function call, the default value is used. It’s also required because the option $str has no default value.

Functions with return values:-

Functions can also return values to the portion of the program where they were called. The value is returned to the program section from where it was named using the return keyword. It is possible to return any type of value, including arrays and objects. The return statement also marks the conclusion of the function, stops it from running, and returns the value.

Parameter passing to Functions:-

In PHP, there are two ways to pass an argument to a function.

Pass by Value:– Moving by value changes the value of an argument within a function, while the original value outside the function stays unaffected. This means that a statement that is a duplicate of the original value is transmitted.

Pass by Reference:– As statements are passed via proxy, the original value is transmitted. As a result, the starting value has changed. Using the ampersand symbol in pass by reference, we just transmit the value’s address, where it is kept (&).

Example:-

Tagged : / / / / / / / / / / / / / /

Understand Ant command line arguments with Examples

ant-command-line-arguments

Several tasks take arguments that will be passed to another process on the command line. To make it easier to specify arguments that contain space characters, nested arg elements can be used.

value – a single command-line argument; can contain space characters.

file – The name of a file as a single command-line argument; will be replaced with the absolute filename of the file.

path – A string that will be treated as a path-like string as a single command-line argument; you can use ; or : as path separators and Ant will convert it to the platform’s local conventions.

pathref – Reference to a path defined elsewhere. Ant will convert it to the platform’s local conventions.

line – a space-delimited list of command-line arguments.

It is highly recommended to avoid the line version when possible. Ant will try to split the command line in a way similar to what a (Unix) shell would do, but may create something that is very different from what you expect under some circumstances.

Examples

<arg value=”-l -a”/>

is a single command-line argument containing a space character.

<arg line=”-l -a”/>

represents two separate command-line arguments.

<arg path=”/dir;/dir2:\dir3″/>

is a single command-line argument with the value \dir;\dir2;\dir3 on DOS-based systems and /dir:/dir2:/dir3 on Unix-like systems.

Command-line Options Summary
ant [options] [target [target2 [target3] …]]

Options:
-help, -h Displays help information describing the Ant command and its options
-projecthelp, -p Print project help information
-version Print the version information and exit
-diagnostics Print information that might be helpful to diagnose or report problems.
-quiet, -q Suppresses most messages not originated by an echo task in the buildfile
-verbose, -v Displays detailed messages for every operation during a build.
-debug, -d Print debugging information
-emacs, -e Produce logging information without adornments
-lib <path> Specifies a path to search for jars and classes
-logfile <file> Use given file for log
-l <file> Use given file for log
-logger <classname> Specifies a class to handle Ant logging.
-listener <classname> Add an instance of class as a project listener
-noinput Do not allow interactive input
-buildfile <file> Use given buildfile
-file <file> Use given buildfile
-f <file> Use given buildfile
-D<property>=<value> Defines a property name-value pair on the command line.
-keep-going, -k execute all targets that do not depend on failed target(s)
-propertyfile <name> load all properties from file with -D properties taking precedence
-inputhandler <class> the class which will handle input requests
-find <file> Search for buildfile towards the root of the filesystem and use it
Tagged : / / / / / / / / / / / / /

Ant command line arguments – Examples – Summary

ant-command-line-arguments

Ant command line arguments
Several tasks take arguments that will be passed to another process on the command line. To make it easier to specify arguments that contain space characters, nested arg elements can be used.

value – a single command-line argument; can contain space characters.
fileThe name of a file as a single command-line argument; will be replaced with the absolute filename of the file.
pathA string that will be treated as a path-like string as a single command-line argument; you can use ; or : as path separators and Ant will convert it to the platform’s local conventions.
pathref Reference to a path defined elsewhere. Ant will convert it to the platform’s local conventions.
line a space-delimited list of command-line arguments.

It is highly recommended to avoid the line version when possible. Ant will try to split the command line in a way similar to what a (Unix) shell would do, but may create something that is very different from what you expect under some circumstances.

Examples
<arg value=”-l -a”/>
is a single command-line argument containing a space character.
<arg line=”-l -a”/>
represents two separate command-line arguments.
<arg path=”/dir;/dir2:\dir3″/>
is a single command-line argument with the value \dir;\dir2;\dir3 on DOS-based systems and /dir:/dir2:/dir3 on Unix-like systems.
Command-line Options Summary
ant [options] [target [target2 [target3] …]]

Options:

-help, -h Displays help information describing the Ant command and its options
-projecthelp, -p Print project help information
-version Print the version information and exit
-diagnostics Print information that might be helpful to diagnose or report problems.
-quiet, -q Suppresses most messages not originated by an echo task in the buildfile
-verbose, -v Displays detailed messages for every operation during a build.
-debug, -d Print debugging information
-emacs, -e Produce logging information without adornments
-lib <path> Specifies a path to search for jars and classes
-logfile <file> Use given file for log
-l <file> Use given file for log
-logger <classname> Specifies a class to handle Ant logging.
-listener <classname> Add an instance of class as a project listener
-noinput Do not allow interactive input
-buildfile <file> Use given buildfile
-file <file> Use given buildfile
-f <file> Use given buildfile
-D<property>=<value> Defines a property name-value pair on the command line.
-keep-going, -k execute all targets that do not depend on failed target(s)
-propertyfile <name> load all properties from file with -D properties taking precedence
-inputhandler <class> the class which will handle input requests
-find <file> Search for buildfile towards the root of the filesystem and use it
Tagged : / / / / / / / / / / / /