Function Syntax in C++
Before diving into the examples, let’s first look at the basic syntax for defining a function in C++:
return_type function_name(parameters) {
// Function body
// perform the task here
}
- return_type: The data type the function will return. If the function doesn't return anything, it’s marked as void.
- function_name: The name of the function, which you use to call it in the program.
- parameters: The values or variables passed into the function to perform its task (also known as arguments). If no parameters are needed, it will be empty.
- function body: The block of code enclosed in { } where the task is performed.
Once the function is defined, you can call it in your program by simply using the function name followed by arguments (if any).
Example 1: Simple Function (No Arguments, No Return Value)
Aim: Define a function that performs a task (prints a message) without taking any input or returning any value.
Code:
#include <iostream>
using namespace std;
// Function declaration
void greet() {
cout << "Hello, World!" << endl;
}
int main() {
greet(); // Function call
return 0;
}
Expected Output:
Hello, World!
Explanation:
- Function: greet() is a simple function that does not take any input or return any output. It simply prints a message "Hello, World!".
- Calling the Function: In main(), we call greet() to execute the code inside it.
- Expected Output: The program will print Hello, World! when executed.
Example 2: Function with Arguments
Aim: Create a function that takes parameters (arguments) as input and performs a task using those arguments.
#include <iostream>
using namespace std;
// Function definition with two parameters
void greet(string name, int age) {
cout << "Hello, " << name << "! You are " << age << " years old." << endl;
}
int main() {
greet("Alice", 30); // Call the greet function with arguments
return 0;
}
Explanation:
- Function: greet() takes two parameters: name (a string) and age (an integer). It prints a personalized greeting message using the provided arguments.
- Calling the Function: We call greet() in main() with the values "Alice" and 30. These are passed as arguments to the function.
- Expected Output: The function will output a greeting with the name and age provided.
Expected Output:
Hello, Alice! You are 30 years old.
Example 3: Function Inside Another Function (Nested Function Call)
Aim: Demonstrate how one function can call another function inside it (nested function calls).
Code:
#include <iostream>
using namespace std;
// Function to calculate the square of a number
int square(int x) {
return x * x; // Returns the square of x
}
// Function to calculate the sum of squares of two numbers
int sumOfSquares(int a, int b) {
return square(a) + square(b); // Calls square() inside to calculate the sum
}
int main() {
int result = sumOfSquares(3, 4); // Call sumOfSquares() with 3 and 4
cout << "Sum of squares: " << result << endl;
return 0;
}
Expected Output:
Sum of squares: 25
Explanation of Result: The program calculates the sum of squares: 3^2 + 4^2 = 9 + 16 = 25.
'C++ Beginner' 카테고리의 다른 글
cpp_006_Introduction to getline and cin.getline in C++: Handling User Input with Spaces (0) | 2025.01.05 |
---|---|
cpp_005_Introduction to Strings (5) | 2025.01.01 |
cpp_004_Introduction to Math Functions (0) | 2025.01.01 |
cpp_003_climits_and_sizeof_in_C++ (0) | 2024.12.25 |
cpp_002_if Statement in C++ (0) | 2024.12.25 |