Functions group reusable logic. Declare return type, name, parameters, and body. Unlike Java methods, C has no classes—functions are file-scope or static for internal linkage.
Definition example
int add(int a, int b) {
return a + b;
}
Parameters are passed by value—copies are made. To modify caller data, pass pointers (covered soon).
Prototypes
Callers should see a prototype before use: int add(int a, int b); at file top or in a header.
Important interview questions and answers
- Q: Pass by value vs reference?
A: C always passes by value; "pass by reference" means passing a pointer to the original object. - Q: void parameter list?
A:void f(void)takes no arguments; empty parentheses in old C mean unspecified parameters—avoid.
Self-check
- How do you return an int from a function?
- Why pass a pointer instead of a large struct by value?
Interview prep
- Pass by value in C?
Arguments are copied; to mutate caller data, pass a pointer to the original object.
- Why prototypes?
Compiler checks argument types and return type at call sites—missing prototypes cause errors or undefined behavior with old-style assumptions.