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
-
The
display
function is first declared at the top of the program using the syntaxvoid 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. -
In the
main()
function, we declare and initialize three variablesx
,y
, andz
that hold the values to be passed to our function. -
The
display
function is then called inside themain()
function using the syntaxdisplay(x, y, z);
. This passes the values ofx
,y
, andz
to the function to be processed. -
The
display
function is defined below themain()
function where it takes these values and prints them out usingstd::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.