C++ Functions - Introduction

Jul 22, 2024

C++ Functions - Introduction

Overview

  • Presenter: Code Beauty
  • Topic: Introduction to C++ Functions
  • Audience: Beginners in C++

Preliminary Actions

  • Subscribe: Subscribe to the channel for future updates.
  • Bell Icon: Click the bell icon for notifications about new videos.
  • Follow: Instagram and Twitter accounts - Code Beauty

What is a Function?

  • A function is a block of code grouped together to solve a specific problem or perform a specific task.
  • Code within the function executes only when the function is called (invoked).
  • Every C++ program contains at least one function: the main function.
  • Execution starts at the first line of main and ends at the last line or a return statement.

Anatomy of a Function

  1. Return Type: Specifies what type of value (if any) the function will return. For now, using void (means no return value).
  2. Function Name: A unique identifier for the function.
  3. Parameters (Arguments): Variables passed into the function (current example uses empty parentheses indicating no parameters).
  4. Function Body: The code block that defines the function's action, enclosed in curly braces {}.

Example of a Simple Function

void myFunction() {
    // Function body
    std::cout << "Hello from function" << std::endl;
}
  • To execute (invoke) the function, call it using its name followed by parentheses: myFunction();

Invoking Functions

  • Before Invocation: Function exists but does not execute.
  • After Invocation: Function executes its code.

Code Example

#include <iostream>

int main() {
    std::cout << "Hello from main" << std::endl;
    myFunction(); // Function invocation
    return 0;
}

void myFunction() {
    std::cout << "Hello from function" << std::endl;
}
  • Output:
    • Hello from main
    • Hello from function

Common Errors

  • Order of Definition: Function must be defined before it is called in main, or a declaration is needed before main with the definition after.
  • Declaration and Definition:
    • Declaration: Inform the compiler about the function's return type, name, and parameters.
    • Definition: Actual code inside the function.

Example of Declaration and Definition

// Function Declaration
void myFunction();

int main() {
    std::cout << "Hello from main" << std::endl;
    myFunction(); // Function invocation
    return 0;
}

// Function Definition
void myFunction() {
    std::cout << "Hello from function" << std::endl;
}
  • This method improves code readability, especially in larger programs.

Benefits of Using Functions

  1. Readability: Easier to manage and read code by grouping tasks into functions.
  2. Reusability: Write the solution code once and invoke it whenever needed.
  3. Modularity: Break down complex problems into simpler, reusable functions.

Conclusion

  • Understanding functions is crucial for effective programming in C++ and other languages.
  • Future videos will cover functions in more detail.

Call to Action

  • Subscribe: Stay tuned for upcoming videos on functions and other topics.
  • Follow: Instagram and Twitter - Code Beauty