Pre-defined Functions in C++

  • Home
  • Pre-defined Functions in C++
Shape Image One

FUNCTIONS

advanced divider

Introduction:

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.

Pre-defined Functions in C++:

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!

User-defined Functions:

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:

  1. Function Declaration:

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);

  1. Function Definition:

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);

Function Definition vs. Function Call:

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 stdint 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 stdint main() {    int number;     cout << “Enter a number: “;    cin >> number;     cout << “The square of “ << number << ” is: “ << number * number << endl;     return 0;}

User-defined Functions vs. Pre-defined Functions:

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()

Quiz

advanced divider