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 prints an array element and its square. Read it from the outside in: `printf` names the function; the parentheses contain four comma-separated arguments; the semicolon ends the statement. section 1.1

A function call hands control from the current function to the called function. Its arguments supply values for that invocation. In the example, the string literal specifies the output format, while `i`, `A[i]`, and `A[i]*A[i]` supply values for its three numeric positions. When the called function returns, execution continues after the call. section 1.1 section 2.4.2 section 17.4

TAKEAWAY: C passes argument values, not the caller’s variables. Passing `i` to `printf` does not let `printf` change `i` itself. Passing an address through a pointer is a separate way to give another function access to an object. section 2.4.2

   extern double fbar(double);

   ...
   double fbar2 = fbar(2)/2;


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

The prototype says `fbar` expects a `double` and returns a `double`. In `fbar(2)/2`, the known parameter type lets the caller convert the integer argument to `2.0` before the call; the return type informs the surrounding expression. section 7.1

Parentheses have different jobs in a call and a declaration: a call contains arguments for one invocation; a prototype declares parameter types and a return type. Use `void` in the parameter list to declare a function called with no arguments, or as the return type when it returns no value. Each call has its own local objects, including when a function calls itself recursively. section 7.1 section 17.4

TAKEAWAY: The call operator `(...)` also applies to function pointers. Even an ordinary-looking call through a function name involves conversion to a function pointer in C’s abstract state machine. A function pointer can therefore select which function a call invokes. section 11.4

How Modern C covers it

How deep it goes
Explains call syntax, value passing, prototype-guided conversion, recursive locals, and function-pointer calls; defers pointer-based access when first introducing calls.
Examples
Uses a formatted `printf` call, a prototype-driven numeric conversion, and equivalent calls through function-pointer expressions.
What it assumes
Introduces calls through `printf` before explaining pointer-based access; later examples assume familiarity with expressions, declarations, and prototypes.
What you can do after
Apply it to read and write calls whose arguments match a prototype, while distinguishing copied values from access through pointers.
How it connects
Connects calls to function prototypes, parameter and return types, call by value, recursion, 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.