Functions in C++

 





A function in C++ is a block of code that performs a specific task. Functions allow you to break down your program into smaller, manageable pieces. You can define a function once and call it multiple times in your program.


1️⃣ Function Syntax in C++

Basic Syntax:

  • return_type function_name(parameters) {

  •     // Function body

  •     // Code to be executed

  •     return return_value;  // if the function has a return type

  • }


  • return_type: The data type of the value the function returns (e.g., int, double, void for no return).

  • function_name: The name of the function.

  • parameters: The values passed to the function (optional). These are also called arguments.


2️⃣ Example: Simple Function

This is a simple example of a function that returns the sum of two numbers.

  • #include <iostream>

  • using namespace std;


  • // Function that takes two integers and returns their sum

  • int sum(int a, int b) {

  •     return a + b;  // Returns the sum of a and b

  • }


  • int main() {

  •     int result = sum(10, 20);  // Call the sum function with arguments 10 and 20

  •     cout << "The sum is: " << result << endl;  // Output the result

  •     return 0;

  • }


Explanation:

  • int sum(int a, int b) is a function that takes two integer arguments (a and b) and returns their sum as an integer.

  • sum(10, 20) calls the function with 10 and 20 as arguments, and the function returns 30.

Output:

  • The sum is: 30



3️⃣ Void Function

A void function doesn't return any value. It simply performs an action without returning any result.

Example: Void Function

  • #include <iostream>

  • using namespace std;


  • // Void function that prints a message

  • void printMessage() {

  •     cout << "Hello, this is a void function!" << endl;

  • }


  • int main() {

  •     printMessage();  // Call the void function

  •     return 0;

  • }


Explanation:

  • void printMessage() is a function that doesn't return any value. It simply prints a message to the console.

  • When printMessage() is called in the main() function, it prints the message.

Output:

  • Hello, this is a void function!



4️⃣ Function with Default Arguments

In C++, you can provide default values for function parameters. If the caller doesn't provide values for these parameters, the default values are used.

Example: Function with Default Arguments

  • #include <iostream>

  • using namespace std;


  • // Function with default arguments

  • void greet(string name = "Guest", int age = 25) {

  •     cout << "Hello, " << name << ". You are " << age << " years old." << endl;

  • }


  • int main() {

  •     greet();  // Calls the function with default arguments

  •     greet("Alice", 30);  // Calls the function with custom arguments

  •     return 0;

  • }


Explanation:

  • void greet(string name = "Guest", int age = 25) defines default values for the parameters name and age.

  • When greet() is called with no arguments, the default values ("Guest" and 25) are used.

  • When greet("Alice", 30) is called, the provided arguments are used instead of the default ones.

Output:

  • Hello, Guest. You are 25 years old.

  • Hello, Alice. You are 30 years old.



5️⃣ Function Overloading

In C++, you can have multiple functions with the same name but different parameter lists. This is called function overloading.

Example: Function Overloading

  • #include <iostream>

  • using namespace std;


  • // Overloaded functions with the same name but different parameters

  • int add(int a, int b) {

  •     return a + b;

  • }


  • double add(double a, double b) {

  •     return a + b;

  • }


  • int main() {

  •     cout << "Sum of integers: " << add(10, 20) << endl;  // Calls the int version

  •     cout << "Sum of doubles: " << add(10.5, 20.3) << endl;  // Calls the double version

  •     return 0;

  • }


Explanation:

  • Two functions named add() are defined: one for integer addition and one for double addition.

  • The appropriate function is called based on the type of the arguments passed.

Output:

  • Sum of integers: 30

  • Sum of doubles: 30.8



6️⃣ Function with Return Value

A function that returns a value is used when you want to compute or calculate something and use that result in your program.

Example: Function with Return Value

  • #include <iostream>

  • using namespace std;


  • // Function that returns the square of a number

  • int square(int num) {

  •     return num * num;

  • }


  • int main() {

  •     int result = square(5);  // Call the function and store the result

  •     cout << "The square of 5 is: " << result << endl;

  •     return 0;

  • }


Explanation:

  • The square() function takes an integer as a parameter, computes its square, and returns the result.

  • In the main() function, the result of square(5) is stored in result and printed.

Output:

  • The square of 5 is: 25



Key Points:

  • Functions allow you to break down your program into smaller, reusable pieces of code.

  • You can use return types to send values back from a function and parameters to pass information to a function.

  • Void functions perform actions without returning a value.

  • Function overloading allows multiple functions with the same name but different parameters.

  • Default arguments provide default values for parameters if no value is passed.

Would you like to dive deeper into more complex functions, such as recursion or lambda functions?


Post a Comment

0 Comments