CSC 1060 Week 11 More about Functions PDF

Document Details

DivineZebra9695

Uploaded by DivineZebra9695

Red Rocks Community College

Tags

programming functions C++ computer science

Summary

This document provides lecture notes on functions in C++ covering topics such as function overloading, parameter passing, recursion, and more. It also includes example codes and exercises for practical application.

Full Transcript

CSC 1060 MORE ABOUT FUNCTIONS OBJECTIVES AGENDA: WEEK 11 Pass arguments by reference and 1. Multi-file Projects understand the differences 2. Macro Guard between pass-by-value and pass- by-reference. 3. Pass by Value...

CSC 1060 MORE ABOUT FUNCTIONS OBJECTIVES AGENDA: WEEK 11 Pass arguments by reference and 1. Multi-file Projects understand the differences 2. Macro Guard between pass-by-value and pass- by-reference. 3. Pass by Value | Pass by Ref Use function overloading and / or 4. Function Overloads default parameters 5. Function Default Arguments Create and Build Multi-File Projects 6. Recursive Functions Demonstrate understanding and 7. Three-Tier Architecture Model use of recursion in a program. 8. TODO Design and implement functions 9. Resources for help using Three-tier architecture model REVIEW Pick 3 Questions to complete from the 5.15 Mixed-Code Exercises Toggle the questions to Parsons Mixed Up Code if showing Active Write Code. HEADER FILES: #INCLUDE Header files are #included and NOT compiled! Whereas source files are compiled and NOT #included! System header files: Files built into C++ #include #include Programmer-defined header files.h: These files are defined by the programmer and contain the declarations (interface) #include "programmerHeader.h" HEADER FILES AND SOURCE FILES What's the difference between header files and source files? Header files are #included, whereas source files are compiled. -NEVER #include "fileName.cpp" file! Header files contain declarations (function prototypes) Source files contain implementation (definitions) Only #include things you need to include Guard against incidental multiple includes with include guards. Review the article, Headers and Includes: Why and How, and name the 3 reasons why we need header files. BUILD (MAKE) A MULTI-FILE PROGRAM 1. Pre-processor - #include header files - the preprocessor replaces the #include by the entire content of the specified header or file. 2. Compiler – compiles each source file (.cpp) checking syntax and if successful, generates corresponding object file (.obj) 3. Linker – links all object files together and generates the application executable file (.exe) Source.cpp #include #include "myfuncts.h" Source.obj main() {} Source.exe Myfuncts.cpp #include "myfuncts.h" Myfuncts.obj Function definitions {} MACRO (INCLUDE) GUARD (CPLUSPLUS) An include Guard (macro guard) is a technique which uses a unique identifier known at pre-processor stage. Macro guards ensure that declarations do NOT get doubly declared If the identifier has NOT been defined (true), the identifier is defined and the items within declared, else it skips over to the endif. All programmer-defined header files MUST wrap an include guard around all function prototypes #ifndef MYFUNCTS_H // if it (identifier) is has not been defined #define MYFUNCTS_H // define the identifier #include // function prototypes; #endif // end the macro PARAMETER PASSING Review the 4.2. Parameter Passing: by Value versus by Reference Complete the multiple-choice questions at the end of 4.2 Q8 to Q9 Stop when you get to 4.3 Arrays as Parameters in Functions PASS BY VALUE PASS BY REFERENCE IN Only! IN and OUT Default way arguments Specified reference: & are passed into functions Address of the argument Copy of the argument 1 X times the memory – points 2 X times the memory to original variable Safe because using a Un-safe - can change original copy To ensure safety - const pass More efficient way of by reference & can be used passing in built-in More efficient way of passing in datatypes like: int, float, objects of classes like: double, char std::string PASS BY REFERENCE EXAMPLE void swap(int& a, int& b); // Prototype Variable: num1 num2 int main(){ int num1 = 10, num2 = 12; Value: 10 12 swap(num1, num2); // Call return 0; Location: 0x3f 0xae } // Definition void swap(int& a , int& b ){ Param: a b int tmp = a; a = b; Value: 0x3f 0xae b = tmp; } Locataion: 0x2b 029c FUNCTION OVERLOADS Same function name Same task to complete Different number of arguments Different data type of arguments Return Type cannot be a differing factor Parameter names cannot be a differing factor Many standard library functions are overloaded in C++. DEFAULT [OPTIONAL] PARAMETERS If a function with default arguments is called without passing arguments the default parameters are used If arguments are passed to call the function the default arguments are ignored The default value is ONLY assigned in the prototype! The default values must be the right most parameters, when a combination of both normal parameters and default parameters are used. RECURSIVE FUNCTION (CPLUSPLUS) Recursion is when a function calls itself Advantages of Recursion It makes our code shorter and cleaner. Recursion is required in problems concerning data structures and advanced algorithms, such as Graph and Tree Traversals Disadvantages of Recursion It takes a lot of stack space compared to an iterative program. It uses more processor time. It can be more difficult to debug compared to an equivalent iterative program. EXAMPLE int main() { int ans = 1; FACTORIAL: int num = 4; ITERATIVELY int i = 0; 1. Circle initalization for(i = num; i >= 1; i--) in BLUE ans *= i; 2. Draw square around stopping std::cout

Use Quizgecko on...
Browser
Browser