0% found this document useful (0 votes)
2 views

comp-prog-module-1

This module teaches students about function return values in C++, focusing on value-returning functions and void functions. It includes examples and activities for implementing functions that return values, such as calculating the square and sum of numbers. Key concepts include the importance of matching return types and ensuring every function path has a return statement.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

comp-prog-module-1

This module teaches students about function return values in C++, focusing on value-returning functions and void functions. It includes examples and activities for implementing functions that return values, such as calculating the square and sum of numbers. Key concepts include the importance of matching return types and ensuring every function path has a return statement.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Module: Understanding Function Return Values in C++

Learning Objectives:
By the end of this module, students should be able to:

1. Understand the concept of function return types and return values.

2. Implement value-returning functions in C++.

3. Differentiate between functions that return a value and those that don’t (void functions).

4. Apply the concept of return values in basic programming problems.

Lesson Content

Part 1: Introduction to Function Return Values

In C++, functions can either:

Return a value (value-returning functions), such as an integer or a string.

Return nothing (void functions), performing an action without giving back any data.

The return type in a function's declaration defines what type of value it will return:

Example: If a function returns an integer, its return type is int.

To return a value, a function must include a return statement with a value of the appropriate type.

Example Code: Value-Returning Function

Here’s a simple function that returns an integer:

#include <iostream>

// This function returns an integer value

int getValueFromUser(){

std::cout << "Enter an integer: ";

int input{};

std::cin >> input;

return input; // returns the entered integer to the caller

int main(){

int num = getValueFromUser(); // Assigns the return value to variable 'num'

std::cout << num << " doubled is: " << num * 2 << '\n';

return 0;

In this example:

getValueFromUser asks the user for a number, and the value entered is returned to the main function.

The return value (the user’s input) is then used in the main function to display double the value.
Key Points
Returning a Value: The return keyword is used to exit a function and provide a value back to the caller.

Return Type Matching: The return type must match the type specified in the function signature (e.g., int for
integer).

One Return Per Call: Each call of a function can only return one value at a time.

Common Issue:

If a value-returning function lacks a return statement, it will cause undefined behavior. Make sure that every
possible path in a value-returning function has a return statement.

Activity
1. Code Practice:

Write a function called squareNumber that asks the user for a number and returns the square of that number.

In the main function, call squareNumber, store the result in a variable, and print “The square is: [result]”.

2. Expected Output:

Students should test their program with several values to check that the function correctly returns the square
of the user’s input.

Homework
Task: Create a small program with the following specifications:

1. Write a function called sumTwoNumbers that:

Asks the user for two integers.

Returns the sum of these two numbers.

Example: The function should return the sum directly to the caller.

2. In main, call sumTwoNumbers and display the result with an appropriate message.

Challenge: Add a second function called multiplyTwoNumbers that:

Asks the user for two integers.

Returns the product of these numbers.

In main, call both sumTwoNumbers and multiplyTwoNumbers and display each result with a message.

Expected Output Example:

Enter first integer for sum: 3

Enter second integer for sum: 4

The sum is: 7

Enter first integer for product: 2

Enter second integer for product: 5

The product is: 10

Notes for Submission:

1. Make sure your program is free of syntax errors.

2. Test your program with different inputs to ensure accuracy

You might also like