---
title: "init(forFunction:arguments:)"
framework: foundation
role: symbol
role_heading: Initializer
path: "foundation/nsexpression/init(forfunction:arguments:)"
---

# init(forFunction:arguments:)

Creates an expression that invokes one of the predefined functions.

## Declaration

```swift
init(forFunction name: String, arguments parameters: [Any])
```

## Parameters

- `name`: The name of the function to invoke.
- `parameters`: An array containing NSExpression objects that will be used as parameters during the invocation of selector. For a selector taking no parameters, the array should be empty. For a selector taking one or more parameters, the array should contain one NSExpression object which will evaluate to an instance of the appropriate type for each parameter. If there is a mismatch between the number of parameters expected and the number you provide during evaluation, an exception may be raised or missing parameters may simply be replaced by nil (which occurs depends on how many parameters are provided, and whether you have over- or underflow).

## Return Value

Return Value A new expression that invokes the function name using the parameters in parameters.

## Discussion

Discussion The name parameter can be one of the following predefined functions.  |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |   |  |  |  This method raises an exception immediately if the selector is invalid; it raises an exception at runtime if the parameters are incorrect. The parameters argument is a collection containing an expression which evaluates to a collection, as illustrated in the following examples: NSNumber *number1 = [NSNumber numberWithInteger:20]; NSNumber *number2 = [NSNumber numberWithInteger:40]; NSArray *numberArray = [NSArray arrayWithObjects: number1, number2, nil];   NSExpression *arrayExpression = [NSExpression expressionForConstantValue: numberArray]; NSArray *argumentArray = [NSArray arrayWithObject: arrayExpression];   NSExpression* expression =     [NSExpression expressionForFunction:@"sum:" arguments:argumentArray]; id result = [expression expressionValueWithObject: nil context: nil];   BOOL ok = [result isEqual: [NSNumber numberWithInt: 60]]; // ok == YES [NSExpression expressionForFunction:@"random" arguments:nil];   [NSExpression expressionForFunction:@"max:"     arguments: [NSArray arrayWithObject:         [NSExpression expressionForConstantValue:             [NSArray arrayWithObjects:                 [NSNumber numberWithInt: 5], [NSNumber numberWithInt: 10], nil]]]];   [NSExpression expressionForFunction:@"subtract:from:"     arguments: [NSArray arrayWithObjects:         [NSExpression expressionForConstantValue: [NSNumber numberWithInt: 5]],         [NSExpression expressionForConstantValue: [NSNumber numberWithInt: 10]], nil]]; Special Considerations This method throws an exception immediately if the selector is unknown; it throws at runtime if the parameters are incorrect.

## See Also

### Creating an Expression for a Function

- [init(forFunction:selectorName:arguments:)](foundation/nsexpression/init(forfunction:selectorname:arguments:).md)
