Difficulty: Beginner to Intermediate | Prerequisites: Java Identifiers, Data Types and Operators notes.
Big picture
Chapter 6 introduces the idea of breaking programs into smaller, self-contained pieces called functions (or methods in Java). This is modular programming, and it is the single biggest organisational principle you will use for the rest of the course and beyond. Without it, every program is one long block of sequential code that is difficult to read, debug and reuse. The exam dedicates a significant portion of its questions to function anatomy (definition, header, prototype, call), scope (local vs global variables), parameter passing (by value vs by reference) and function overloading. If you are coming into this cold, make sure you are comfortable with data types and identifiers first.
Modular programming splits a program into manageable functions, each handling one task. A function has a definition (the logic), a header (the signature line) and can have a prototype (a forward declaration). Functions can return a value or be void. Variables declared inside a function are local; those outside are global (and best avoided). Java passes primitive arguments by value (a copy), so the original is unchanged. Function overloading lets you reuse a name if the parameter lists differ. Stubs and drivers help you test individual pieces.
Modular programming
The practice of breaking a program into a set of manageable, self-contained functions, each responsible for one task. The alternative is linear (sequential) programming, where everything runs top to bottom in one block. In simple terms, instead of one giant recipe, you write a separate mini-recipe for each step.
Function definition
The complete implementation of a function: its header plus its body (the statements that do the work). The definition is where the actual logic lives. Think of it as the full instructions, not just the label on the box.
Function header
The first line of a function definition. It includes the access modifier, return type, function name and parameter list with identifier names. Example: public static double calculate(int x, int y). In simple terms, it is the function's signature line that tells you everything about what goes in and comes out.
Function prototype
A declaration that tells the compiler about a function's return type and parameter types before the full definition appears. Parameter names are optional in a prototype. It ends with a semicolon. Think of it as a promise to the compiler: "this function exists, here is what it looks like, I will define it later."
Function call
The statement that executes a function by its name, passing in any required arguments. Example: double result = calculate(5, 10);.
void function
A function that performs a task but does not return a value to the caller. Its return type is the keyword void. In simple terms, it does something but hands nothing back.
Parameter
A variable declared in the function header that receives data when the function is called. Parameters are defined at the time you write the function. Think of it as the slot waiting to be filled.
Argument
The actual value or variable passed to a function when it is called. Arguments fill the parameter slots. In simple terms, the parameter is the placeholder, the argument is what you plug into it.
Pass by value
The function receives a copy of the argument's data. Changes inside the function do not affect the original variable. This is how Java handles all primitive types.
Pass by reference
The function receives access to the original variable's memory location, so changes inside the function do affect the original. In Java, object references are passed by value (the reference is copied, but it still points to the same object), which gives a similar effect for objects.
Local variable
A variable declared inside a function body. It exists only while that function is executing and cannot be accessed from outside.
Global variable
A variable declared outside all functions, accessible from anywhere in the program. Best practice is to limit their use, as they make code harder to debug and maintain.
Static local variable
A local variable that retains its value between function calls, rather than being reinitialised each time the function runs.
Function overloading
Having two or more functions with the same name but different parameter lists (different number of parameters, different types, or both). The compiler uses the parameter signature to decide which version to call. Return type alone is not enough to distinguish overloaded functions.
Driver
A test module designed to call and test other functions or modules. It exercises the code being tested.
Stub
A dummy function that stands in for a real function during testing, allowing you to verify that calls and returns work correctly without implementing the full logic.
exit()
A function (in Java, System.exit()) that terminates the entire program immediately, regardless of where it is called. Unlike return, which exits only the current function, exit() stops everything.
Modular vs linear programming
Linear programming writes all logic in one continuous block, top to bottom. It works for tiny programs but becomes unmanageable as complexity grows.
Modular programming breaks the program into functions, each handling a specific task. Benefits include improved manageability (complex problems become simpler sub-tasks) and code reusability (call the same function multiple times without rewriting it).
Additional benefits: easier debugging (test one function at a time), easier collaboration (different people write different functions) and clearer organisation.
Anatomy of a function
The header (or signature) declares the return type, the function name and the parameter list: public static double calculate(int x, int y)
Return type: double (what the function hands back)
Name: calculate
Parameters: int x and int y (what the function takes in)
The definition includes the header plus the body (the curly-brace block containing the logic).
A prototype is a forward declaration: it tells the compiler the function exists, listing return type and parameter types, ending in a semicolon. Parameter names are optional in the prototype but required in the header.
A call is where you invoke the function: double answer = calculate(3, 7);
void vs value-returning functions
A void function performs an action but returns nothing. You cannot assign its result to a variable.
A value-returning function specifies a return type (int, double, String, etc.) and must include a return statement that hands back a value of that type.
A function can take zero to many parameters but can return either zero (void) or one value.
Scope: local, global and static variables
A local variable is declared inside a function. It is created when the function is called and destroyed when the function ends. It cannot be accessed from outside.
A global variable is declared outside all functions. It is accessible everywhere, but good practice is to avoid globals because they make bugs harder to trace.
A static local variable is a local variable that retains its value between calls. It is initialised only once and persists for the life of the program.
Pass by value vs pass by reference
Pass by value: the function receives a copy of the argument. Any changes inside the function do not affect the caller's original variable. Java passes all primitive types by value.
Pass by reference: the function accesses the original variable's memory location. Changes inside the function are reflected in the caller's variable. In Java, object references are passed by value, but because the copy still points to the same object, modifications to the object's fields are visible to the caller.
Function overloading
Two or more functions can share the same name if their parameter lists differ (different number of parameters, different types, or both).
The compiler distinguishes them by parameter signature, not by return type. You cannot overload by changing only the return type.
All overloaded functions must exist within the same class scope.
Testing: stubs and drivers
A driver is a small program that calls and tests a function or module. It provides test inputs and checks outputs.
A stub is a dummy function standing in for a real one. It lets you verify that the calling mechanism works before the real logic is written.
exit() (or System.exit() in Java) terminates the entire program from wherever it is called, unlike return which exits only the current function, or break which exits only the current loop or switch.
Students confuse parameters and arguments. The parameter is the variable in the function header; the argument is the actual value you pass when calling the function.
Students think a void function returns zero or null. It returns nothing at all.
Students assume global variables are fine to use freely. Best practice is to restrict globals because they make programs harder to debug.
Students believe static local variables lose their value between calls. They do not; that is the whole point of marking them static.
Students think changing the return type alone is enough to overload a function. The compiler distinguishes overloaded functions by their parameter lists, not their return types.
Modular programming (Q5, Q55) is a definition question worth easy marks. Know the term and two benefits: manageability and reusability.
The difference between a function header and a prototype (Q7, Q32) is a favourite exam question. Remember: the prototype does not require parameter names; the header does.
void functions (Q8, Q23) come up in both multiple-choice and true/false. A void function returns no value, not zero.
Local vs global scope (Q9, Q31, Q35) and pass by value (Q30, Q49) appear frequently. Know the memory behaviour.
Function overloading rules (Q10, Q53) are tested in both short and long-answer formats. The three requirements: same name, different parameter lists, return type alone is not sufficient.
Stubs, drivers and exit() (Q18, Q38, Q45) are tested as definitions.
True or false: A void function returns zero. Answer: False. It returns nothing.
Fill in the blank: A variable declared inside a function body is called a ___ variable. Answer: local
True or false: Static local variables lose their value between function calls. Answer: False. They retain their value.
What distinguishes overloaded functions from each other? Answer: Their parameter lists (number or types of parameters).
True or false: A function prototype requires parameter names. Answer: False. Only the types are required.
Q: What type of programming involves breaking a program into manageable functions?
A: Modular programming.
Q: Which part of a function provides the statements that make up its logic?
A: The function definition (specifically, the body within the definition).
Q: What is the primary difference between a function header and a function prototype?
A: The header requires identifier names for parameters; the prototype does not necessarily require them.
Q: Briefly explain the difference between a parameter and an argument.
A: A parameter is the variable declared in the function header that receives data. An argument is the actual value passed to the function when it is called.
Q: In terms of memory, what happens when a variable is passed by reference?
A: The function accesses the original argument's memory location rather than a copy, so changes inside the function affect the original value.
Q: What is the purpose of a stub in software testing?
A: A stub is a dummy function that stands in for a real function, used to test that the calling and return mechanisms work correctly.
Q: Identify the return type and parameters for: public static double calculate(int x, int y).
A: Return type is double. Parameters are two int values, x and y.
Q: Contrast modular programming with linear programming, citing two benefits of functions.
A: Modular programming breaks a program into smaller functions, while linear programming flows sequentially without abstraction. Two benefits: (1) improved manageability by reducing complex problems into simpler tasks, and (2) code reusability, allowing functions to be called multiple times without rewriting logic.
Functions are the foundation for object-oriented programming (Chapter 7), where methods are simply functions that belong to a class. Constructor methods, accessor (get) methods and mutator (set) methods are all functions with specific roles. Understanding scope and parameter passing also connects to how arrays are handled when passed to functions.
CSE110 functions, modular programming Java, function definition vs prototype, function header vs prototype, void function Java, pass by value Java, pass by reference Java, local variable scope, global variable bad practice, static local variable, function overloading rules, method overloading Java, stub testing, driver testing, System.exit Java, ASU CSE110 Chapter 6, Principles of Programming functions