Function (mathematics)

A function f is commonly declared by stating its domain X and codomain Y using the expression

$$f: X \rightarrow Y$$

or

{displaystyle X~{stackrel {f}{rightarrow }}~Y.}

In this context, the elements of X are called arguments of f. For each argument x, the corresponding unique y in the codomain is called the function value at x or the image of x under f. It is written as f(x). One says that f associates y with x or maps x to y. This is abbreviated by

$$y = f(x).$$

Function  (Computer Science)

Subprograms may be defined within programs, or separately in libraries that can be used by multiple programs. In different programming languages, a subroutine may be called a procedure, a function, a routine, a method, or a subprogram. The generic term callable unit is sometimes used.
The content of a subroutine is its body, which is the piece of program code that is executed when the subroutine is called or invoked.

A subroutine may be written so that it expects to obtain one or more data values from the calling program (its parameters or formal parameters). The calling program provides actual values for these parameters, called arguments.

In computer programming, a function prototype or function interface is a declaration of a function that specifies the function's name and type signature (aritydata types of parameters, and return type), but omits the function body. While a function definition specifies how the function does what it does (the "implementation"), a function prototype merely specifies its interface, i.e. what data types go in and come out of it.

Syntax

A C function definition consists of a return type (void if no value is returned), a unique name, a list of parameters in parentheses, and various statements:

<return-type> functionName( <parameter-list> )
{
    <statements>
    return <expression of type return-type>;
}

A function with non-void return type should include at least one return statement. The parameters are given by the <parameter-list>, a comma-separated list of parameter declarations, each item in the list being a data type followed by an identifier: <data-type> <variable-identifier>, <data-type> <variable-identifier>, ....

If there are no parameters, the <parameter-list> may be left empty or optionally be specified with the single word void.

 

#include <stdio.h>
 
 /* If this prototype is provided, the compiler will catch the error in
  * int main(). If it is omitted, then the error may go unnoticed.
  */
 int myfunction(int n);              /* Prototype */
 
 int main(void) 
{
/* Calling function */ printf("%d\n", myfunction()); /* Error: forgot argument to myfunction */ return 0; }

/* Called function definition */
int myfunction(int n)
{

if (n == 0) return 1;
else
return n * myfunction(n - 1);
}

You have no rights to post comments