Structures and Functions
Passing structures by value, returning them, and when copying a struct starts to cost.
Records Cross the Boundary
Functions and structs were made for each other: a function that takes a struct Point receives one idea, not two loose ints, and its prototype documents the fact. The rules at the call boundary are the ones you already own, pass-by-value and return-and-assign, applied to whole records, plus one new cost consideration that pointers will later resolve.
Passing a Struct: The Whole Record Copies
Pass-by-value applies member by member: the parameters a and b are complete, independent copies of home and office, and the function scribbling on them would leave the caller's records untouched, the chapter 9 rule, now moving whole structs per call. The distance program from the defining-structures exercise is now a main that orchestrates and a function that computes: same distance, better shape.
Returning a Struct
Functions can return structs whole, which is how "compute a new record" reads best:
The function builds a local record and returns it by value; the caller's total receives a copy via ordinary struct assignment. This is return-and-assign wearing a record, and it scales to any function whose answer is naturally one struct: the constructor-style makePoint(x, y) helper, chapter 12's file records, the linked-list nodes to come.
The Cost Column
Copying a struct Point is eight bytes, nothing. Copying a struct Student with a name array and ten scores is real work, per call, and a 10,000-record table passed by value would copy megabytes. The honest engineering summary, one chapter early: small structs pass and return by value gracefully; large ones want their address passed instead, which costs pointer machinery you meet next chapter. Until then, course examples keep by-value structs small, and the exam question "what is the drawback of passing structures to functions?" has its answer: the whole record is copied each call.
The comparison worth making: an array argument never copies (chapter 8's arrays-pass-without-& behavior, fully explained next chapter), but a struct argument always does, even when the struct contains an array. Passing a struct holding char name[50] copies all fifty bytes; two facts that look contradictory until pointers reconcile them.
Prototypes and Ordering
Struct-taking functions need the struct type declared before their prototype or definition, so the file order becomes: struct definitions first, then functions, then main. This top-of-file cluster of type definitions is the seed of the header files arriving in the multi-file lesson, where shared types move into .h files precisely so every file's functions can see them.
Key Takeaways
- Struct parameters are complete copies: pass-by-value member by member, caller records untouched.
- Functions return structs whole; the caller stores them by struct assignment, return-and-assign with records.
- Small structs travel by value happily; large ones copy their full size per call, the drawback exams ask for, resolved by address-passing next chapter.
- Structs always copy even when arrays inside them ride along; bare arrays never do: pointers explain both.
- Struct types are defined above the functions that mention them, the file shape that headers later formalize.
How did you find this lesson?
Your rating helps us improve the content.
Create an account to track your progress and access interactive exercises. Already have one? Sign in.
Structures and Functions - Quiz
Test your understanding of the lesson.
Practice Exercises
Point Arithmetic, Properly Factored
Write struct Point addPoints(struct Point a, struct Point b) returning the member-wise sum, and a main that reads two points, calls the function, and prints the result. Structs in, a struct out, main only orchestrating.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!