Podcast
Questions and Answers
What is the primary characteristic of a void function call?
What is the primary characteristic of a void function call?
What is a key difference between a value-return function and a void function?
What is a key difference between a value-return function and a void function?
What is the main benefit of using library functions in programming?
What is the main benefit of using library functions in programming?
What does a function definition require?
What does a function definition require?
Signup and view all the answers
Which of the following statements best describes the purpose of functions in programming?
Which of the following statements best describes the purpose of functions in programming?
Signup and view all the answers
Study Notes
CSC 1060 Functions
- Functions allow a program to be more meaningful, specifying the task clearly.
- Functions perform step-wise refinement, focusing on a single task.
- Functions allow code reuse, modularizing the program.
- Functions hide individual instruction details, summarizing the task into a module.
- Functions are their own IPO algorithms (Input, Process, Output).
- Pre/IN: initial data needed by the function.
- Post/OUT: answer returned from the function.
Objectives
- Design and implement functions using stepwise refinement.
- Develop reusable code that is modular, easy to read, easy to debug, and easy to maintain.
- Determine the scope of local variables.
- Use function prototypes to declare functions and define functions using the function header.
Agenda Week 10
- Overview of built-in library functions
- Function Prototypes
- Function Calls
- Overview Programmer-Defined Functions
- Type of Functions Arguments and Return Type
- 3-Steps to a function
- Structure of main
- Scope of Identifiers
- TODO & Resources for help
Pre-Challenge: Functions Matching
- Hide computation details by defining functions.
- Function definition requires:
- A name
- A group of parameters (input)
- A return type (output) - may return a variable, a value, or nothing (void)
- A body (process)
Library Built-in Functions
- One of the key reasons to use library functions is that they work.
- They are optimized for maximum performance.
- Development time is saved because the functions are already developed and tested.
- These functions allow applications to work consistently across different computers (portable).
Prototype Declaration
- In C++, all elements must be declared before use.
- For built-in functions, include the appropriate standard library.
- Function prototypes specify the necessary information to call and use a function, including return type, function name, parameter types, and parameter names.
- 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, identified by its name.
- Functions with common tasks are grouped in the same library file.
- Examples of built-in functions:
-
pow
: Calculates powers. -
toupper
: Converts a character to uppercase. -
isalpha
: Checks if a character is alphabetical.
-
Function Parameters
- Data the function needs to perform the job or pre-conditions
- Prototype specifies the formal parameter list with a placeholder specifying the type of data to be input (argument).
- Function call passes actual data (argument)
- Order and data type of actual arguments passed MUST match the order and data types of the parameter list.
Function Return
- Return type is the answer coming out of the function (post-condition).
- 1 or 0 answers can be returned from a function.
- Value Returning Function: non-void function
- Void Function: no return – void
Value-Returning Function Calls
- Value-returning function call executes the function, returns a single answer.
- This answer is returned through the return type.
- Function call must assign the returned value to a variable. This variable must be of the same data type as the return type of the function being called.
Void Function Calls: No Return
- Void Function call executes the function but does not return anything.
- No answer comes out of the function.
- The function is just called by its name.
Random Machine in C++
- Pseudo-random number generator is used to generate random numbers.
- The following libraries and built-in functions allow for random number generation in C++:
-
#include <ctime>
-
#include <cstdlib>
(forsrand
,rand
)
-
Time Function:
- Value-Return Function Call:
time_t seedValue = time(nullptr);
- Pre: timer as
time_t*
.nullptr
works as if nothing is accepted. - Post: seconds as
time_t
- Task: number of seconds elapsed since January 1, 1970 00:00:00 GMT.
Srand Function:
- Void Function Call:
srand(unsigned int seed);
- Pre: seed as unsigned int.
- Post: void – N/A
- Task:
srand
prevents random numbers from being the same each time the program runs.
Rand Function:
- Value-Return Function Call:
int num = rand();
- Pre: N/A
- Post: random number as
int
- Task: returns a pseudo-random integer value between 0 (inclusive) and
RAND_MAX
(defined by the compiler).
Pre-Challenge
- Complete multiple choice exercises Q1 to Q10.
- Skip Q5, Q6 (Boolean values).
- Skip Q11, Q12 (recursion).
User-Defined Function Types
- Four types:
- Example 1: Function with NO Pre and NO Post (avoid).
- Example 2: Function with NO Pre and Post (input-only, one return value).
- Example 3: Function with Pre-conditions and NO Post (output-only).
- Example 4: Function with Pre-conditions and Post (follows IPO, value-returning).
3-Step Programmer-Defined Functions (C++)
-
- Declare the function (prototype).
- Required for all functions
- Declare after includes, before
main
(or in a header file).
-
- Call the function (execution).
-
- Define the function (implementation).
- Defined after
main
(or in a separate source file .cpp). - Never nest function definitions within another function.
Structure of Main
- Program info with name and summary.
- Include system library.
- Include programmer-defined header.
- Function prototypes.
- Declared constants (instead of global variables).
- Definition of main program.
- Function calls and return 0
- Function header and implementation.
Scope of Identifiers
- Review the tutorial on Name Visibility (scope).
- Namespace
std
is not allowed. - Identify three different variable types.
- Differentiate variable scope.
- Specify which variable scope is not allowed.
Earn Your Pre-Work Grade
- Post weekly discussion question and research solution to D2L.
- Complete Week 10 Content Module in D2L to 100%.
Questions/Clarifications/Help
- Student Office Hours: By Appointment, Drop-in times available.
- Email: [email protected]
- RRCC On-Campus Tutoring: [link]
- 24/7 Online Tutoring: D2L > Content > Resources for Help.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
This quiz focuses on the concepts of functions in programming, highlighting their importance in creating modular, reusable code. It covers function prototypes, calls, and types of function arguments while emphasizing stepwise refinement and local variable scope.