OneBite.Dev - Coding blog in a bite size

Declare A Function With Multiple Parameter In C++

Code snippet for how to Declare A Function With Multiple Parameter In C++ with sample and detail explanation

C++ is a powerful programming language that allows for easy declaration and use of functions with multiple parameters. This brief article aims to provide clear guidance on how you can declare a function with multiple parameters in C++.

Code Snippet: Declaring a Function with Multiple Parameters

Here is a simple example of a function declaration in C++ with multiple parameters:

#include<iostream>

// function declaration
void display(int a, float b, char c);

int main() {
    int x = 10;
    float y = 20.5;
    char z = 'A';

    // function call
    display(x, y, z);

    return 0;
}

// function definition
void display(int a, float b, char c) {
    std::cout << "Integer is: " << a << std::endl;
    std::cout << "Float is: " << b << std::endl;
    std::cout << "Char is: " << c << std::endl;
}

This code includes a function named “display” that takes three parameters: an integer, a float, and a character.

Code Explanation for Declaring a Function with Multiple Parameters

  1. The display function is first declared at the top of the program using the syntax void display(int a, float b, char c);. This tells the compiler that a function exists which is going to take three parameters: an integer, a float, and a character.

  2. In the main() function, we declare and initialize three variables x, y, and z that hold the values to be passed to our function.

  3. The display function is then called inside the main() function using the syntax display(x, y, z);. This passes the values of x, y, and z to the function to be processed.

  4. The display function is defined below the main() function where it takes these values and prints them out using std::cout.

Remember, the function declaration (also known as the function prototype) lets the compiler know that a function exists, even before it is used. The function definition, however, contains the actual code that will be executed when the function is called. Both the declaration and definition include the data types of the parameters, ensuring that the right kind of data is passed to the function.

c-plus-plus