Function Return
Overview​
A function return specifies the value or result that a function sends back to the part of the program that called it. The return value is used to pass a result from the function execution to its caller. If no return value is specified, some languages return a default value (such as void
in C++ or None
in Python).
Syntax​
C++​
// Function returning a value
return_type function_name(parameter1_type parameter1) {
// function body
return value;
}
Example​
C++ Example​
// Function returning an integer
int add(int a, int b) {
return a + b;
}
// Function call and storing the return value
int result = add(10, 20); // result is now 30
Syntax​
Python​
# Function returning a value
def function_name(parameter1):
# function body
return value
Example​
Python Example​
# Function returning a value
def add(a, b):
return a + b
# Function call and storing the return value
result = add(10, 20) # result is now 30
Key Points:​
- Return value is the result that a function sends back to the caller after its execution.
- In C++, the return type is declared in the function signature (e.g., int for returning integers, void for no return value).
- Python functions return None if no explicit return statement is provided.
- The return keyword is used in both C++ and Python to send a value back to the caller.
- Multiple values can be returned from a function in some languages (e.g., using tuples in Python).