CSC 1060 Week 11 More Functions
5 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the primary purpose of header files in C++?

  • To contain implementations of functions.
  • To compile source files into executable formats.
  • To provide function prototypes and declarations. (correct)
  • To define global variables for the entire program.
  • Which statement is true regarding the use of include guards in header files?

  • They ensure all header files are compiled at runtime.
  • They compile the source files before linking.
  • They are used to include multiple source files.
  • They prevent multiple inclusions of the same header file. (correct)
  • What is the correct order of actions performed by the pre-processor and compiler when building a multi-file program?

  • Compile all header files, replace includes, link executable files.
  • Compile source files, replace includes, generate object files.
  • Replace includes, compile each source file, generate object files. (correct)
  • Generate object files, replace includes, compile source files.
  • Which of the following options describes a difference between pass-by-value and pass-by-reference?

    <p>Pass-by-value creates a copy of the value, pass-by-reference uses the original variable directly.</p> Signup and view all the answers

    What is one major benefit of using function overloading in programming?

    <p>It enables functions to perform different tasks based on the same name.</p> Signup and view all the answers

    Study Notes

    Course Objectives

    • Pass arguments by reference and understand the difference between pass-by-value and pass-by-reference.
    • Use function overloading and/or default parameters.
    • Create and build multi-file projects.
    • Demonstrate understanding and use of recursion in a program.
    • Design and implement functions using a three-tier architecture model.

    Agenda Week 11

    • Multi-file projects
    • Macro Guard
    • Pass by Value / Pass by Reference
    • Function Overloads
    • Function Default Arguments
    • Recursive Functions
    • Three-Tier Architecture Model
    • TODO
    • Resources for help

    Review

    • Pick 3 questions from 5.15 Mixed-Code Exercises.
    • Toggle questions to Parsons Mixed Up Code if Active Write Code is showing.

    Header Files

    • Header files are #included and NOT compiled.
    • Source files are compiled and NOT #included.
    • System header files are built into C++. Examples: <iostream>, <string>.
    • Programmer-defined header files have a .h extension. Example: "programmerHeader.h"

    Header Files and Source Files

    • Header files are #included, while source files are compiled.
    • Never #include "fileName.cpp".
    • Header files have declarations (function prototypes).
    • Source files contain implementation (definitions).

    Build (Make) a Multi-File Program

    • Pre-processor replaces #include with header file content.
    • Compiler compiles each source file (.cpp) and checks syntax, creating object files (.obj).
    • Linker links object files together to create the executable file (.exe).

    Macro (Include) Guard (C++ plus)

    • An include guard (macro guard) uses a unique identifier at the pre-processor stage.
    • Macro guards prevent double declarations.
    • If the identifier is not defined, the code block inside is included; otherwise, skipped.
    • All programmer-defined header files must wrap an include guard around function prototypes.
    • Example #ifndef MYFUNCTS_H ... #endif.

    Parameter Passing

    • Review 4.2 Parameter Passing (by value versus by reference).
    • Complete multiple-choice questions at the end of 4.2 (Q8 to Q9).
    • Stop when reaching 4.3 Arrays as Parameters in Functions.

    Pass by Value vs. Pass by Reference

    • (Pass by Value)*
    • Default argument passing method.
    • A copy of the argument is passed.
    • Uses 2X memory.
    • Safe, as original is not modified.
    • Efficient for built-in datatypes (int, float, double, char).
    • (Pass by Reference)*
    • Uses the address of the argument.
    • Uses 1X memory.
    • Unsafe, allows modification of original.
    • Requires & (e.g., int &a).

    Pass by Reference Example

    • Demonstrates how pass by reference works in C++.

    Function Overloads

    • Same function name.
    • Same task, different arguments/data types.
    • Return type and parameter names aren't factors.
    • Standard library functions are often overloaded.

    Default Parameters

    • Functions can have default values for parameters.
    • Defaults are ignored if argument is supplied.
    • Default value assignment occurs in the prototype.
    • Defaults must be on the right side in the list of parameters.

    Recursive Functions

    • A function calling itself.
    • Can shorten and clarify code.
    • Essential for operations with data structures (e.g., graph, tree traversals).
    • Disadvantages of recursion: uses more space/time, and can be more difficult to debug.

    Example Factorial (Iterative)

    • Illustrates an iterative calculation of a factorial.

    Example Factorial (Recursive)

    • Illustrates a recursive calculation of a factorial.

    Three-Tier Architecture Model

    • Application architecture using 3 logical/physical tiers:
      • Presentation tier (user interface)
      • Application tier (logic and processing)
      • Data tier (data storage and management).
    • Functions should be designed according to the three-tier architecture model.

    Earn Your Pre-Work Grade

    • Post discussion question & research to D2L.
    • Complete Week 11 content module at 100%.

    Questions/Clarifications/Help

    • Student office hours scheduled with Julie.
    • Drop-in times available (on campus).
    • Remote via Zoom possible.
    • Email: [email protected]
    • RRCC campus tutoring available.
    • 24/7 online tutoring resources in D2L.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    More Like This

    Use Quizgecko on...
    Browser
    Browser