C++ Functions Notes PDF

Summary

These notes provide an overview of functions in C++. They cover key concepts like local variables, arguments, and parameters, highlighting the importance of modular code and static local variables. The notes also include examples and concepts of passing arguments by reference and returning values.

Full Transcript

**Chapter 6 Notes: Functions in C++** **Key Concepts** 1. **Local Variables and Function Calls** - Local variables are allocated on the function's stack frame. - The stack frame is destroyed when the function returns, losing all local variable values. - A fresh set of...

**Chapter 6 Notes: Functions in C++** **Key Concepts** 1. **Local Variables and Function Calls** - Local variables are allocated on the function's stack frame. - The stack frame is destroyed when the function returns, losing all local variable values. - A fresh set of local variables is created on the next function call. 2. **Argument vs. Parameter** - **Parameter:** A variable declared in the function's definition (e.g., int x in void func(int x)). - **Argument:** The actual value passed to the function during a call (e.g., func(10)). 3. **Parameter Variables** - Defined in the function's header, inside parentheses. - Example: void func(int param) { }. 4. **Protecting Arguments from Modification** - Pass by value (default in C++) to create a copy. - Use const with references: void func(const int &x). 5. **Order of Arguments** - Arguments in a function call must match the order and types in the function definition. 6. **Returning Values** - Use return followed by an expression: return x;. 7. **Advantages of Modular Code** - Easier maintenance, readability, debugging, and code reuse. 8. **Static Local Variables** - Retain their values between function calls. - Useful for storing persistent data or counts. 9. **Passing Arguments by Reference** - Allows the function to modify the original variable. - Example: void swap(int &a, int &b). **Fill-in-the-Blank Concepts** 1. The **function header** shows the function name, return type, and parameter list. 2. A function that doesn't return a value uses the **void** return type. 3. Either a function\'s **prototype** or its definition must precede all calls to the function. 4. Values sent into a function are called **arguments**. 5. Special variables that hold copies of arguments are **parameters**. 6. Passing only a copy of an argument is called **pass by value**. 7. A **function prototype** eliminates the need for defining a function before calling it. 8. A **local variable** is defined within a function and inaccessible outside it. 9. **Global variables** are accessible by all functions and initialized to 0 by default. **True or False** 1. **True:** Functions should have names reflecting their purpose. 2. **False:** Function headers end with braces, not semicolons. 3. **True:** Function prototypes end with semicolons. 4. **True:** Programs always begin execution at main(). 5. **False:** Functions return to the point of the call, not necessarily main(). 6. **True:** Argument order in the call must match the parameter order in the function. 7. **True:** Parameter scope is limited to its function. 8. **False:** Function parameters only modify arguments if passed by reference or pointer. 9. **True:** Parameter names in prototypes are optional. 10. **True:** Local variables can have the same names as global variables. **Algorithm Workbench Examples** 1. **Example Call** void showValue(int quantity); showValue(10); 2. **Function for Half Value** double half(double number) { return number / 2.0; } 3. **Using a Function** int result = cube(4); // cube(int num) { return num \* num \* num; } 4. **Function to Multiply by Ten** void timesTen(int number) { cout \ 100); } **Common Errors in Functions** 1. **Invalid Void Function** void total(int value1, value2, value3) { return value1 + value2 + value3; } // Errors: Parameter types missing; void can\'t return. int total(int value1, int value2, int value3) { return value1 + value2 + value3; } 2. **Average Calculation** double average(int value1, int value2, int value3) { return (value1 + value2 + value3) / 3.0; } 3. **Default Arguments in Wrong Order** int area(int length = 30, int width = 10) { return length \* width; } 4. **Incorrect Reference Syntax** void getValue(int &value) { cin \>\> value; } **Chapter 7 Notes: Arrays in C++** **Key Concepts** 1. **Size Declarator vs. Subscript** - **Size Declarator:** Specifies the total number of elements an array can hold. Example: int values\[10\]; (size declarator is 10). - **Subscript:** Index used to access a specific array element. Example: values\[0\] (subscript is 0). 2. **Memory Usage** - Each element\'s memory size is determined by the data type. - Example: int values\[10\]; (if int is 4 bytes, the total memory used is 10 \* 4 = 40 bytes). 3. **Array Initialization** - Can be done with an initializer list: int values\[5\] = {1, 2, 3, 4, 5};. - Uninitialized elements are set to 0 if the array is static or global. 4. **Accessing Elements** - Array indexing starts at 0 and ends at size - 1. - Example: For int values\[5\] = {4, 7, 6, 8, 2};: - values\[0\] is 4. - values\[4\] is 2. 5. **Passing Arrays to Functions** - Arrays are passed by reference (as a pointer to the first element). - The function must also accept the array size to avoid out-of-range access. 6. **Two-Dimensional Arrays** - Declared with two size declarators: double sales\[8\]\[10\];. - Access elements using row and column indices: sales\[0\]\[1\]. 7. **Vectors (STL) vs. Arrays** - Vectors are dynamic, resizeable, and have member functions like size() and push\_back(). - Arrays have a fixed size and lack such flexibility. **Fill-in-the-Blank Concepts** 1. **Size Declarator and Subscript** - The **size declarator** specifies how many elements an array can hold. - The **subscript** is used to access specific elements. 2. **Initialization and Memory** - If an array is **partially initialized**, uninitialized elements are set to 0. - A two-dimensional array requires **two size declarators** for definition. 3. **Array and Function Relationship** - To pass an array to a function, pass its **name**. - A **two-dimensional array** passed to a function must specify the size of all dimensions except the first. 4. **Vectors** - A **vector** is a sequence container in the STL. - Use push\_back to add elements and pop\_back to remove the last element. **True or False** 1. **True:** Arrays cannot be resized once defined. 2. **False:** The first element of an array is accessed by the subscript 1 (it\'s 0). 3. **True:** Arrays do not perform bounds checking in C++. 4. **True:** An array can be partially initialized. 5. **True:** A two-dimensional array is like several arrays put together. **Examples and Algorithm Workbench** 1. **Accessing and Initializing Arrays** int numbers\[5\] = {1, 2, 3}; // numbers\[3\] and numbers\[4\] will be initialized to 0. cout \ 3. **Accessing Structure Members** - **Dot Operator (.):** Used for direct access. s1.name = \"John\"; - **Arrow Operator (-\):** Used when accessing members through a pointer. Student \*ptr = &s1; ptr-\name = \"Alice\"; 4. **Structure Initialization** - Structures can be initialized when declared: cpp Copiar código Student s2 = {\"Alice\", 102, 3.9}; 5. **Nested Structures** - Structures can have other structures as members. struct Address { string city; string street; }; struct Student { string name; Address addr; }; Student s; s.addr.city = \"New York\"; 6. **Arrays of Structures** - Useful for storing multiple records of the same type. Student classList\[3\] = { {\"John\", 101, 3.8}, {\"Alice\", 102, 3.9}, {\"Bob\", 103, 3.7} }; cout \

Use Quizgecko on...
Browser
Browser