In C++, functions are like mini-programs within your main program. Each function tackles a specific job, like calculating an area or printing a message. Think of them as separate tools in your coding toolbox, each designed for a specific task. By using functions, you can break down complex problems into smaller, easier-to-manage steps, making your code cleaner and more organized. Let’s explore how functions work in C++ and how they can make your programming journey smoother.
In C++, you don’t always have to write everything from scratch. Instead, you can leverage the power of pre-defined functions that are already built into the language. These functions perform various tasks, saving you time and effort in your coding endeavors.
Think of them as helpful tools in your C++ toolbox, ready to be used whenever you need a specific task completed. Let’s explore some common pre-defined functions and how they can be incorporated into your programs!
While pre-defined functions offer a variety of functionalities, sometimes you’ll need to create your own custom tools. That’s where user-defined functions come in. These are functions that you, the programmer, define to perform specific tasks within your program.
Here are the key components of user-defined functions:
This acts like an announcement, telling the compiler about the existence of your function without providing its implementation details. It includes the function name, parameter list (if any), and return type (optional) written in this format:
C++
return_type function_name(parameter_list);
This is where the magic happens! You define the actual code your function will execute when called. It includes the function declaration again, followed by the function body enclosed in curly braces {}. The body contains the statements responsible for completing the function’s task.
iii. Function Call:
This is how you “use” your function to perform its task. You write the function name followed by parentheses containing the actual values (arguments) you want to pass, if the function requires any.
Example in C++:
C++
// Function declarationdouble calculate_area(double length, double width); // Function definitiondouble calculate_area(double length, double width) { return length * width;} // Function call (assuming variables length and width are defined with values)double rectangle_area = calculate_area(length, width);
Feature | User-defined Functions | Pre-defined Functions |
---|---|---|
Origin | Created by the programmer | Already defined in the C++ library |
Declaration | Required | Not required |
Definition | Required | Not required, already defined |
Flexibility | Can be customized | Limited customization options |
Examples | calculate_area(), add_numbers() | cout(), cin(), sqrt() |
C++
#include <iostream> using namespace std; int add_numbers(int a, int b) { return a + b;} int main() { int num1, num2, sum; cout << “Enter two numbers: “; cin >> num1 >> num2; sum = add_numbers(num1, num2); cout << “The sum is: “ << sum << endl; return 0;}
Example Program (Pre-defined Function):
C++
#include <iostream> using namespace std; int main() { int number; cout << “Enter a number: “; cin >> number; cout << “The square of “ << number << ” is: “ << number * number << endl; return 0;}
Feature | User-defined Functions | Pre-defined Functions |
---|---|---|
Origin | Created by the programmer | Already defined in the C++ library |
Declaration | Required | Not required |
Definition | Required | Not required, already defined |
Flexibility | Can be customized | Limited customization options |
Examples | calculate_area() , add_numbers() |
cout() , cin() , sqrt() |
0 of 10 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 10 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
Which of the following is a common pre-defined function in C++ for finding the absolute value of a number?
What is the purpose of the toupper() function in C++?
Which of the following is a standard library function in C++ for generating random numbers?
What does the strlen() function do in C++?
Which pre-defined function in C++ is used to concatenate two strings?
Which component of a user-defined function acts like an announcement, informing the compiler about the function’s existence?
In a user-defined function, where is the actual code written to execute the function’s task?
What is the purpose of a function call in C++?
Which of the following is NOT a key component of a user-defined function?
What does the function declaration include in C++?