Lesson 22: Function Arguments and Return Values
Parameters and Arguments
- Parameters: Variables defined in the function definition/prototype that receive values.
- Arguments: The actual values passed to the function when it is called.
Functions with Arguments, Returning a Value
c float circle_area(float radius) { // radius is the parameter return 3.14159 * radius * radius; } // Call: float r = 4.0; float area = circle_area(r); // r is the argument
Functions with No Arguments, Returning a Value
Use void in the parameter list if no arguments are expected.
c float get_pi(void) { return 3.14159; }
Functions with Arguments, Returning void
void is used as the return type when a function performs an action but doesn't need to return a value.
c void print_message(char *msg) { printf("Message: %s\n", msg); return; // Optional return statement for void functions }
The return Statement
- It exits the function immediately, returning control to the caller.
- It sends a value back to the caller (if the function return type is not
void). - The type of the returned value must match the function's declared return type (or be convertible to it).