liveBook · Manning

Call

As explained in Modern C

A function call is an expression or statement that invokes a function and supplies any required arguments.

17    printf("element %zu is %g, \tits square is %g\n",
18           i,
19           A[i],
20           A[i]*A[i]);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":2}],[{\"line\":0,\"ch\":6},{\"line\":0,\"ch\":12}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":2}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":2}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}]]"}
!@%STYLE%@!

This call asks printf to print an array element and its square. The format string and three expressions are four arguments, separated by commas inside parentheses. section 1.1

A function call invokes a function and passes it argument values. The calling function’s execution is suspended while control passes to the called function; on return, execution continues after the call. A call can also produce a value for use in an expression. section 2.4.2 section 7.1 section 17.4

In the printf example, the format string specifies where to insert the values of i, A[i], and A[i]*A[i]. The expressions supply values, not the variables themselves. C uses call by value: printf cannot change i merely because i is an argument. Passing an address through a pointer is a separate mechanism. section 1.1 section 2.4.2

A function prototype declares the parameter types and return type used at a call. For example, with the declaration extern double fbar(double);, the call fbar(2) converts the integer argument 2 to double. The resulting double can participate in a larger expression, such as fbar(2)/2. A void parameter list in a prototype specifies no parameters; a void return type specifies no return value. section 7.1

Each call has its own local variables, including when a function calls itself recursively. Separate calls can nevertheless affect shared data through pointers, so separate local variables do not guarantee independent effects. section 7.4 section 17.4

double f(double a);

// Equivalent calls to f, steps in the abstract state machine
f(3);        // Decay → call
(&f)(3);     // Address of → call
(*f)(3);     // Decay → dereference → decay → call
(*&f)(3);    // Address of → dereference → decay → call
(&*f)(3);    // Decay → dereference → address of → call


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":6}]]"}
!@%STYLE%@!

The call operator applies to function pointers. In the abstract state machine, even the ordinary spelling f(3) converts the function designator to a pointer before the call; the listed forms are equivalent calls. section 11.4

A function pointer also permits a function to be chosen dynamically. Its function type must match the expected interface: a comparison function with incompatible parameter types is not suitable for qsort. section 11.4

How Modern C covers it

How deep it goes
Explains control transfer, value passing, prototypes, recursion, and pointer decay, with pointer-based variable access deferred initially.
Examples
Uses formatted array output, a converted numeric argument, equivalent function-pointer calls, and recursive state to illustrate calls.
What you can do after
Evaluate and understand how C calls pass values, use prototypes, and invoke functions through pointers.
How it connects
Connects function calls to prototypes, argument conversion, recursion, local variables, and function pointers.

Also explained in

Compare how 7 books explain call →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.