Notes of Chapter 5 FUNTIONS
Notes of Chapter 5 FUNTIONS
Functions
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:
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.
• 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");
Output: