Returning Values With Simple Data Types
Functions can return values of simple data types such as integers, floating-point numbers, and characters. To specify the return type of a function, you indicate the data type before the function name in the function signature.
When the function is executed, it can compute a value, which is then returned using the return statement. This type of return value was used in the code examples before:
main.cpp
12345678910111213141516171819#include <iostream> // Function that adds two integers and returns the result int add(const int a, const int b) { int sum = a + b; return sum; } int main() { int a = 3; int b = 5; // Call the function and store the returned result in a variable int result = add(a, b); std::cout << result << std::endl; }
The add() function is declared to return an integer value by using the int specifier before the function name. It calculates the sum of a and b and returns the result as an int.
Ensure that the variable where you intend to store the returned value inside the main() block matches the data type of the corresponding return value.
Please note that the function's return value can be specified only within the function signature. Even if you try to return a value of a different type using the return statement, it will be automatically cast to the data type declared in the function signature:
main.cpp
1234567891011121314151617181920#include <iostream> // Function that adds two integers and returns the result int add(const double a, const double b) { double sum = a + b; return sum; } int main() { double a = 3.5; double b = 5.1; // Call the function and store the returned result in a variable int result = add(a, b); // Print the result std::cout << result << std::endl; }
The sum inside the function is of type double, but the function’s return type is int.
As a result, the returned value is converted to an integer, giving 8 instead of 8.6.
Note that we can return only one value from a function using a simple datatype specifier. To return multiple values, we should use arrays or custom structures (classes).
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Awesome!
Completion rate improved to 5
Returning Values With Simple Data Types
Pyyhkäise näyttääksesi valikon
Functions can return values of simple data types such as integers, floating-point numbers, and characters. To specify the return type of a function, you indicate the data type before the function name in the function signature.
When the function is executed, it can compute a value, which is then returned using the return statement. This type of return value was used in the code examples before:
main.cpp
12345678910111213141516171819#include <iostream> // Function that adds two integers and returns the result int add(const int a, const int b) { int sum = a + b; return sum; } int main() { int a = 3; int b = 5; // Call the function and store the returned result in a variable int result = add(a, b); std::cout << result << std::endl; }
The add() function is declared to return an integer value by using the int specifier before the function name. It calculates the sum of a and b and returns the result as an int.
Ensure that the variable where you intend to store the returned value inside the main() block matches the data type of the corresponding return value.
Please note that the function's return value can be specified only within the function signature. Even if you try to return a value of a different type using the return statement, it will be automatically cast to the data type declared in the function signature:
main.cpp
1234567891011121314151617181920#include <iostream> // Function that adds two integers and returns the result int add(const double a, const double b) { double sum = a + b; return sum; } int main() { double a = 3.5; double b = 5.1; // Call the function and store the returned result in a variable int result = add(a, b); // Print the result std::cout << result << std::endl; }
The sum inside the function is of type double, but the function’s return type is int.
As a result, the returned value is converted to an integer, giving 8 instead of 8.6.
Note that we can return only one value from a function using a simple datatype specifier. To return multiple values, we should use arrays or custom structures (classes).
Kiitos palautteestasi!