Istruzione Return nelle Funzioni
L'istruzione return termina l'esecuzione di una funzione e restituisce un valore di un tipo predefinito.
function.h
12345int func() // int - predefined { int variable = 10; return variable; // variable = 10 }
Se il tipo è specificato in modo errato, la funzione si comporterà in modo imprevedibile.
main.cpp
12345678910111213#include <iostream> unsigned short func() { return -10; } // The unsigned short data type has no negative values. int main() { std::cout << func() << std::endl; }
Ovvero, prima di creare una funzione, deve essere specificato il tipo di dato che restituisce. Inoltre, in C++ esistono funzioni speciali di tipo void. Le funzioni di questo tipo di dato possono non restituire nulla:
first_example.cpp
second_example.cpp
123456789101112#include <iostream> void voidFunction() { std::cout << "It's void function!" << std::endl; // Function without return } int main() { voidFunction(); }
All'interno delle funzioni possono esserci più istruzioni di return, e ciascuna verrà eseguita solo in determinate condizioni.
main.cpp
1234567891011121314151617#include <iostream> int func() { int a = 50; int b = 6; if (a > b) // If `a > b`, func will `return a` return a; else // Otherwise func will `return b` return b; } int main() { std::cout << func() << std::endl; // Func calling }
Se sono presenti due istruzioni di return, la seconda verrà ignorata:
main.cpp
123456789101112131415#include <iostream> int func() { int a = 50; // Declare variable a int b = 6; // Declare variable b return a; // Function stops here, b is never returned return b; // Unreachable } int main() { std::cout << func() << std::endl; // Call func and print result }
Grazie per i tuoi commenti!
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Can you explain what a void function is in more detail?
What happens if a function doesn't have a return statement?
Can you give an example of a function with multiple return statements?
Awesome!
Completion rate improved to 3.85
Istruzione Return nelle Funzioni
Scorri per mostrare il menu
L'istruzione return termina l'esecuzione di una funzione e restituisce un valore di un tipo predefinito.
function.h
12345int func() // int - predefined { int variable = 10; return variable; // variable = 10 }
Se il tipo è specificato in modo errato, la funzione si comporterà in modo imprevedibile.
main.cpp
12345678910111213#include <iostream> unsigned short func() { return -10; } // The unsigned short data type has no negative values. int main() { std::cout << func() << std::endl; }
Ovvero, prima di creare una funzione, deve essere specificato il tipo di dato che restituisce. Inoltre, in C++ esistono funzioni speciali di tipo void. Le funzioni di questo tipo di dato possono non restituire nulla:
first_example.cpp
second_example.cpp
123456789101112#include <iostream> void voidFunction() { std::cout << "It's void function!" << std::endl; // Function without return } int main() { voidFunction(); }
All'interno delle funzioni possono esserci più istruzioni di return, e ciascuna verrà eseguita solo in determinate condizioni.
main.cpp
1234567891011121314151617#include <iostream> int func() { int a = 50; int b = 6; if (a > b) // If `a > b`, func will `return a` return a; else // Otherwise func will `return b` return b; } int main() { std::cout << func() << std::endl; // Func calling }
Se sono presenti due istruzioni di return, la seconda verrà ignorata:
main.cpp
123456789101112131415#include <iostream> int func() { int a = 50; // Declare variable a int b = 6; // Declare variable b return a; // Function stops here, b is never returned return b; // Unreachable } int main() { std::cout << func() << std::endl; // Call func and print result }
Grazie per i tuoi commenti!