Parameters and Return Values
Functions in Programming can take parameters, which are values you supply to the function so that the function can do something utilizing those values. These parameters are placed inside the parentheses in the function declaration.
Functions in Programming can also return a value back to the caller. The return type is defined in the function declaration. Inside the function, the return statement is used to specify the return value.
#include <iostream>
using namespace std;
// Function with parameters and return value
int add(int a, int b) { return a + b; }
int main()
{
int sum = add(5, 3);
cout << "The sum is " << sum;
return 0;
}
public class Main {
// Function with parameters and return value
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int sum = add(5, 3);
System.out.println("The sum is " + sum);
}
}
# Function with parameters and return value
def add(a, b):
return a + b # Return the sum of a and b
# Main function
def main():
sum = add(5, 3) # Call the add function with 5 and 3
print("The sum is", sum) # Print the sum
# Call the main function
if __name__ == "__main__":
main()
// Function with parameters and return value
function add(a, b) {
return a + b;
}
// Main function
function main() {
let sum = add(5, 3);
console.log("The sum is " + sum);
}
// Call the main function
main();
// This code is contributed by Shivam Gupta
Output
The sum is 8
Functions in Programming
Functions in programming are modular units of code designed to perform specific tasks. They encapsulate a set of instructions, allowing for code reuse and organization. In this article, we will discuss about basics of function, its importance different types of functions, etc.
Table of Content
- What are Functions in Programming?
- Importance of Functions in Programming
- Functions Declaration and Definition
- Calling a Functions in Programming
- Parameters and Return Values
- Built-in Functions vs. User-Defined Functions
- Recursion in Functions
- Tips for Functions in Programming
- Conclusion