CSC 1060 Week 10 Functions PDF
Document Details
Uploaded by DivineZebra9695
Red Rocks Community College
Tags
Summary
This document is instructional material on computer science topics, particularly functions in C++. It covers various aspects of C++ functions providing definitions, examples, and different types of functions.
Full Transcript
CSC 1060 FUNCTIONS OBJECTIVES AGENDA: WEEK 10 Design and implement functions 1. Overview of built-in library using stepwise refinement. functions 2. Function Prototypes Develop reusable code that is...
CSC 1060 FUNCTIONS OBJECTIVES AGENDA: WEEK 10 Design and implement functions 1. Overview of built-in library using stepwise refinement. functions 2. Function Prototypes Develop reusable code that is 3. Function Calls modular, easy to read, easy to debug, and easy to maintain. 4. Overview Programmer- Defined Functions Determine the scope of 5. Type of Functions Arguments and local variables. Return Type Use function prototypes to 6. 3-Steps to a function declare functions and 7. Structure of main define functions using the 8. Scope of Identifiers function header 9. TODO & Resources for help PRE-CHALLENGE: FUNCTIONS MATCHING Complete the 4.7 Matching the function term with the definitions Hide the details of any computation by defining a function. A function definition requires: a name a group of parameters (IN) a return type (OUT) – that may either: return a variable return a value or nothing (specified by the keyword void) a body (PROCESS) OVERVIEW OF FUNCTIONS Functions allow a program to be more meaningful as the function name should specify the task more clearly. Functions perform step-wise refinement and focus on a single task Functions allow the program to re-use code instruction blocks, modularizing a program Functions hide individual instruction details and summarize the task into one module. Functions are their own IPO algorithms Pre / IN : initial data that the function needs to perform the job Post / OUT : answer returned out of the function LIBRARY BUILT-IN FUNCTIONS (PROGRAMIZ) One of the most important reasons you should use library functions is simply because they work! These functions were developed using the most efficient code optimized for maximum performance. Development time is saved as the developer doesn't have "to re-invent the wheel" as the functions are already developed and tested. With ever-changing real-world needs, your application is expected to work every time, everywhere. And, these library functions help you in that they do the same thing on every computer making them portable. PROTOTYPE - DECLARATION (PROGRAMIZ) In C++, everything MUST be declared before being used. For built- in functions, #include which in turn includes the declaration of the functions. Function prototypes specify everything needed to know in order to call and use the function. returnType task( type param1, type param2,... ); In – ( formal parameter list - pre-conditions ) Process – task Out – return type of value coming out - post-condition FUNCTION TASK (PROCESS) A function performs a single task and is identified by the name of the function representing the task Functions are grouped with common tasks in the same library file. Here are examples of common built-in function tasks. pow – calculates and returns the answer of the base number raised to the power. toupper – takes a single character and returns the uppercase character equivalent. isalpha – determines if the single character entered is an alphabetical character or not. FUNCTION PARAMETERS (PROGRAMIZ) Data that the function needs to perform the job or pre-conditions Prototype specifies the formal parameter list with a place holder specifying the type of data to pass IN. Function call passes in the actual data (argument) The order and datatype of the actual arguments passed in MUST match the order and datatypes of the parameter list FUNCTION RETURN (PROGRAMIZ) Return type is the answer coming OUT of the function or post- condition 1 or 0 (No) answers can be returned out of a function 1 answer returned OUT Value Returning Function: non-void function 0 or No answer coming out Void Function – no return: void VALUE-RETURNING FUNCTION CALLS Value-returning function call – executes of the function A value-returning function has a single answer being returned out of the function through the return type. The function call must DO something with the value being returned and should be assigned to a variable of the same data type as the returnType of the function being called. postVar = taskName ( preArg, preArg2, … ) ; Which function is NOT a value-returning function? How do you know? VOID FUNCTION CALLS: NO RETURN Void Function call – executes of the function and do NOT return anything out No answer comes out of the function; therefore, the function is NOT assigned to a variable and is just called by the function name. taskName ( preArg, preArg2, … ) ; What code instruction is generally not part of a void function RANDOM MACHINE IN C++ Pseudo-random number generator is used to generate random numbers The following libraries and built-in functions allow a random number to be generated in C++ #include // time #include // srand, rand TIME FUNCTION: returnType task (formal parameter list); time_t time ( time_t* timer ); Value-Return Function Call: time_t seedValue = time ( nullptr ); Pre: timer as time_t* - nullptr will work as if it accepted nothing and return the same value Post: seconds as time_t Task: number of seconds that have passed since 00:00:00 GMT January 1, 1970 as time_t SRAND FUNCTION: returnType task (formal parameter list); void srand ( unsigned int seed ); Void Function Call: unsigned int seedValue = (unsigned int) time(nullptr); srand ( seedValue ) ; Pre: seed as unsigned int Post: void – N/A Task: srand prevents random numbers from being the same every time the program is executed and to allow more pseudo-randomness. RAND FUNCTION: returnType task (formal parameter list); int rand ( ); Value-Return Function Call: int num = rand ( ) ; Pre: N/A - nothing Post: random number as int Task: returns a pseudo-random value between(inclusive) 0 and and RAND_MAX (defined by the compiler) PRE-CHALLENGE Complete the 5.14 Multiple Choice Exercises Q1 to Q10 SKIP Q5 and Q6 (boolean values) SKIP Q11 and Q12 (recursion) USER-DEFINED FUNCTION TYPES (PROGRAMIZ) 1. Example 1- Function with NO Pre and NO Post Generally AVOID writing these type of functions, as they do not follow step- wise refinement. The function is forced to do more than the single task of the function and combines tier 1 and tier 2 functionality 2. Example 2- Function with NO Pre and Post returned out Good structure for INPUT only function, where one value is returned. 3. Example 3- Function with Pre condition(s) and NO Post Good structure for OUTPUT only functions, but make sure the function doesn't both perform a task and print as this goes against the rule of one function = one task. 4. Example 4- Function with Pre condition(s) and Post returned out Follows true IPO algorithm and is standard for value-returning functions 3-STEP PROGRAMMER-DEFINED FUNCTIONS (CPLUSPLUS) 1. -Declare the function – writing the function prototype; For all functions, a prototype is required NOT optional! Declared after includes and above main (NOT inside of main) OR Declared in separate header file.h 2. -Call the function when the task needs to execute 3. -Define the function – write the function implementation Never nest function definitions within another function Defined after main block (NOT inside of main) OR Defined in separate source file.cpp STRUCTURE #include < system library > #include " programmer defined header.h " 1. Function prototypes – declarations of functions ; OF MAIN // declared constants: const - NO GLOBAL VARIABLES! int main() { // definition of main program 2. Function calls return 0; } 3. Function header + implementations { // definition of function task } SCOPE OF IDENTIFIERS Review the tutorial on Name Visibility (Scope) We will not be covering Namespaces and remember using namespace std; is NOT allowed in our programs What are the three different types of variables? What differentiates the scope of the variables? Which one is NOT allowed for our class? EARN YOUR PRE-WORK GRADE Post your weekly discussion question and research solution to D2L TODO Complete Week 10 Content Module in D2L to 100% WHAT'S COMING UP NEXT...WEEK 11 QUESTIONS | CLARIFICATIONS | HELP Student Office Hours: Schedule Meeting with Julie o By Appointment (both on-campus and remote via Zoom) o Drop-In Times Available (on-campus) Email: [email protected] RRCC On Campus Tutoring: https://www.rrcc.edu/learning- commons/tutoring 24/7 Online Tutoring: D2L > Content > Resources for Help