Declaration, Type and Return
From our previous lesson, you learned the basic anatomy of functions:
python912345678func_type func_name(arguments_placeholder){// the core functionality of the functionblock_of_code;// the resulting outcome after function executionreturn function_output;}
Declaration
Before utilizing a function, it has to be declared. Calling a function before its declaration will result in an error.
You can either declare the entire function simultaneously with its definition (as illustrated above) or use a prototype, which we'll touch upon as a bonus at the end of this segment.
Function Types and Return Values
The function's type informs the compiler about the datatype of the function's outcome. For instance, if our function calculates the sum of two integers, then we anticipate an integer as the result:
python912345678int sumFunction(){int number1 = 6;int number2 = 10;int result = number1 + number2;return result;}
Note
If a function type isn't specified, the compiler defaults to treating it as an
int
.
A mismatch between the data type and the return type might render the function (or even the whole program) malfunctioning. Here's an example: let's add two non-integer numbers, but set the function to return an integer type:
python912345678int addFunction(){double number1 = 6.985;double number2 = 231.465;double result = number1 + number2;return result; // this will lead to an incorrect result}
When summing non-integer values, you'd expect a precise outcome. But due to the type inconsistency, the result is erroneous.
Main
123456789101112131415161718#include <stdio.h> int func() { double number1 = 6.985; // not int nubmer double number2 = 231.765; // not int number double result = number1 + number2; // not int number too return result; // output of function } int main() { // Call and immediately printing the result of our function printf("Expected Result is 238.75, but result is %d\n", func()); return 0; }
Two primary factors contribute to this error:
An incorrect return type (should be
double
);Using the wrong format specifier (
%d
) in theprintf()
function (should be%f
).
The correct version would be:
Main
123456789101112131415161718#include <stdio.h> double func() { double number1 = 6.985; // int nubmer double number2 = 231.765; // int number double result = number1 + number2; // int number too return result; // output of function } int main() { // We immediately print the result of our function printf("Expected Result is 238.75, and result is %f\n", func()); return 0; }
Note
The outcome of any function will typically be a numeric or string value.
This implies that we can directly assign the function's result to variables/arrays or even pass it to other functions. An example of this is how we used the result with the printf
function in the scenarios above.
Note
%f
is the format specifier for floating-point numbers.
It's also worth mentioning that a function can house multiple return
statements, with each activating under distinct conditions.
Merci pour vos commentaires !
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion