{"record":{"id":"64e19f437fc8860a","repo":"TheAlgorithms/Python","slug":"differentiate-requires-a-function-as-input-for-f","errorCode":null,"errorMessage":"differentiate() requires a function as input for func","messagePattern":"differentiate\\(\\) requires a function as input for func","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/dual_number_automatic_differentiation.py","lineNumber":119,"sourceCode":"    >>> differentiate(lambda y: 0.5 * (y + 3) ** 6, 3.5, 4)\r\n    7605.0\r\n    >>> differentiate(lambda y: y ** 2, 4, 3)\r\n    0\r\n    >>> differentiate(8, 8, 8)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: differentiate() requires a function as input for func\r\n    >>> differentiate(lambda x: x **2, \"\", 1)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: differentiate() requires a float as input for position\r\n    >>> differentiate(lambda x: x**2, 3, \"\")\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: differentiate() requires an int as input for order\r\n    \"\"\"\r\n    if not callable(func):\r\n        raise ValueError(\"differentiate() requires a function as input for func\")\r\n    if not isinstance(position, (float, int)):\r\n        raise ValueError(\"differentiate() requires a float as input for position\")\r\n    if not isinstance(order, int):\r\n        raise ValueError(\"differentiate() requires an int as input for order\")\r\n    d = Dual(position, 1)\r\n    result = func(d)\r\n    if order == 0:\r\n        return result.real\r\n    return result.duals[order - 1] * factorial(order)\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    import doctest\r\n\r\n    doctest.testmod()\r\n\r\n    def f(y):\r\n        return y**2 * y**4\r","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/dual_number_automatic_differentiation.py#L101-L137","documentation":"Raised by differentiate() in maths/dual_number_automatic_differentiation.py when the func argument is not callable. The routine drives automatic differentiation by evaluating func(Dual(position, 1)) and reading the resulting dual parts, so it must receive an actual function; anything else fails the callable(func) check with ValueError.","triggerScenarios":"Calling differentiate('x**2', 3.0, 1), differentiate(42, 2.0, 1), or passing a result instead of a function like differentiate(x**2, 2.0, 1).","commonSituations":"Passing a string expression instead of a lambda; passing the already-computed value instead of the function object; passing an object whose class lacks __call__; confusion between a function reference f and a call f(x).","solutions":["Pass a callable: differentiate(lambda x: x ** 2, 3.0, 1) or a named def.","If you have a string expression, first convert it with eval in a controlled namespace or use a symbolic library — do not hand the raw string to differentiate().","Drop the parentheses when passing named functions: f not f(x)."],"exampleFix":"# before\ndifferentiate('x**2', 3.0, 1)   # ValueError\n\n# after\ndifferentiate(lambda x: x ** 2, 3.0, 1)  # 6.0","handlingStrategy":"type-guard","validationCode":"if not callable(func):\n    raise TypeError(f'func must be callable, got {type(func).__name__}')","typeGuard":"def is_func(f) -> bool:\n    return callable(f)","tryCatchPattern":"try:\n    d = differentiate(func, position, order)\nexcept ValueError as e:\n    if 'function as input' in str(e):\n        raise TypeError('pass a lambda or def, not an expression result') from e\n    raise","preventionTips":["Pass f, not f(x); pass lambda x: expr for inline expressions.","Never hand raw expression strings to numeric routines — compile them first."],"tags":["maths","autodiff","validation","callable","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}