0% found this document useful (0 votes)
41 views4 pages

Notes of Chapter 5 FUNTIONS

The document provides an overview of functions in C++, including the use of header files, the purpose of the void keyword, and the differences between passing arguments and returning values. It also lists five standard built-in functions with examples, outlines the advantages of user-defined functions, and addresses common errors in function prototypes and output formatting. Overall, it emphasizes the importance of modularity, reusability, and organization in programming.

Uploaded by

umerkg95
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)
41 views4 pages

Notes of Chapter 5 FUNTIONS

The document provides an overview of functions in C++, including the use of header files, the purpose of the void keyword, and the differences between passing arguments and returning values. It also lists five standard built-in functions with examples, outlines the advantages of user-defined functions, and addresses common errors in function prototypes and output formatting. Overall, it emphasizes the importance of modularity, reusability, and organization in programming.

Uploaded by

umerkg95
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/ 4

Notes for CS:

Functions

Short questions and answers:


Q2: Why do we use header files?

A: Header files (.h files) are used to declare the interfaces to a set of functions and
data structures without exposing the implementation details. They allow code to
be modular and easier to manage by separating the function declarations
(prototypes) and macro definitions from the actual code (which is usually in .c
or .cpp files). This helps in:

• Code Organization: Keeps code clean and organized.


• Reusability: Functions and definitions in header files can be reused across
multiple source files.
• Separation of Concerns: Distinguishes between interface (what functions
do) and implementation (how they do it).

Q1: What is the purpose of the keyword void in a function?

• A: The keyword void is used in function declarations and definitions to


indicate that the function does not return a value. It tells the compiler and
anyone reading the code that the function performs its task without
providing any data back to the caller. For example, void displayMessage()
declares a function display Message that doesn't return anything.

Q3: Difference between passing arguments and returning values from functions?

• A: Passing Arguments: This is when you send data to a function when you
call it. The function uses these inputs to perform its task. For example, in
int add(int a, int b), a and b are arguments passed to the function add.
• Purpose: To provide necessary data for the function to operate.
• Returning Values: This is when a function sends data back to the caller
after completing its task. The returned value can then be used by the caller.
For example, int result = add(5, 3); stores the return value of add(5,
3) in result.
• Purpose: To provide the result of the function's task to the caller.

Long questions answers:


Q4: list the five standard built in functions with example?

• A: cout:
• Purpose: Used to output (print) data to the standard output stream
(console).
• Example: std::cout << "Hello, World!" << std::endl;
• cin:
• Purpose: Used to read input from the standard input stream (keyboard).
• Example: std::cin >> number; // Reads an integer input
• getline():
• Purpose: Reads a line of text from an input stream.
• Example: std::getline(std::cin, str); // Reads a whole line into the
string str
• strlen():
• Purpose: Returns the length of a C-style string (excluding the null
terminator).
• Example: int len = std::strlen("Hello"); // len would be 5
• atoi():
• Purpose: Converts a string to an integer.
• Example: int num = atoi("12345");

Q7: Write down the advantages of user-defined functions in C++?

A: Advantages of User-Defined Functions in C++:

1. Modularity and Improved Readability:


a. Breaking down a large program into smaller, manageable functions
makes it easier to handle and understand.
b. Organizing code into functions enhances readability, allowing others
to follow the program’s flow easily.
2. Code Reusability and Collaboration:
a. Functions can be written once and reused multiple times throughout
the program, reducing redundancy.
b. Multiple programmers can work on different functions
simultaneously without interfering with each other’s work, making
team projects more efficient.
3. Easier Debugging and Maintenance:
a. Functions help isolate different parts of the code, making it easier to
debug and test individual sections.
b. Updates and changes can be made more efficiently, as modifying a
function doesn’t affect the rest of the program if the interface
remains consistent.
4. Encapsulation and Abstraction:
a. Functions help encapsulate logic and data, making the program more
modular and reducing dependencies between different parts of the
code.
b. Functions enable abstraction by hiding complex logic behind a simple
interface, allowing users to use the function without understanding
its internal workings.
5. Enhanced Collaboration and Public Interface:
a. Distinguishes between the public interface (what functions do) and
private implementation (how they do it).
b. Promotes better organization and documentation, helping teams
collaborate more effectively.

Q8 get the output of the following function:


A: Errors and corrections:

1. Function prototype typo:


a. function protype should be function prototype.
2. Escape sequence issues in cout statements:
a. "/n should be "\n for new line.
b. "/t" should be "\t" for tab.
c. "/n;" should be "\n";.
3. Incorrect cin operator:
a. cin << m; should be cin >> m;.

Output:

If you enter 5 for m, the output will be:


The value of m..........5
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50

You might also like