ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Programming Fundamentals
    COMP1112
    Progress0 / 19 topics
    Topics
    1. Introduction to Problem Solving2. Von-Neumann Architecture3. Introduction to Programming4. Role of Compiler and Linker5. Introduction to Algorithms6. Basic Data Types and Variables7. Input/Output Constructs8. Arithmetic, Comparison and Logical Operators9. Conditional Statements and Execution Flow10. Repetitive Statements and Execution Flow11. Lists and Memory Organization12. Multi-dimensional Lists13. Introduction to Modular Programming14. Function Definition and Calling15. Stack Rolling and Unrolling16. Strings and String Operations17. Pointers/References18. Static and Dynamic Memory Allocation19. File I/O Operations
    COMP1112›Function Definition and Calling
    Programming FundamentalsTopic 14 of 19

    Function Definition and Calling

    4 minread
    632words
    Beginnerlevel

    Function Definition and Calling in C++

    Functions are a fundamental concept in programming that allow you to encapsulate reusable code, making your programs more organized and easier to manage. In C++, functions can be defined and called in various ways, and understanding this concept is crucial for effective programming.

    1. What is a Function?

    A function is a block of code designed to perform a specific task. It can take inputs, process them, and return an output. Functions help break down complex problems into smaller, manageable pieces.

    2. Function Definition

    A function definition includes the function's return type, name, parameters (if any), and the body of the function.

    Syntax:

    returnType functionName(parameterType1 parameterName1, parameterType2 parameterName2, ...) {
        // Function body
        // Code to execute
        return value; // Optional, based on return type
    }
    

    Example:

    #include <iostream>
    using namespace std;
    
    // Function to add two integers
    int add(int a, int b) {
        return a + b; // Return the sum
    }
    

    3. Function Declaration (or Prototype)

    A function declaration (or prototype) informs the compiler about the function's name, return type, and parameters before its actual definition. This is useful for organizing code and resolving dependencies.

    Syntax:

    returnType functionName(parameterType1, parameterType2, ...);
    

    Example:

    int add(int a, int b); // Function declaration
    

    4. Function Calling

    To execute a function, you "call" it by using its name and passing the required arguments. The call can occur from any part of the program as long as the function is declared.

    Syntax:

    functionName(argument1, argument2, ...);
    

    Example:

    int main() {
        int num1 = 5, num2 = 10;
        int sum = add(num1, num2); // Function call
        cout << "Sum: " << sum << endl; // Output the result
        return 0;
    }
    

    5. Complete Example

    Here’s a complete example that combines function declaration, definition, and calling:

    #include <iostream>
    using namespace std;
    
    // Function declaration
    int add(int a, int b);
    int subtract(int a, int b);
    
    int main() {
        int num1 = 15, num2 = 5;
    
        // Function calls
        cout << "Addition: " << add(num1, num2) << endl;
        cout << "Subtraction: " << subtract(num1, num2) << endl;
    
        return 0;
    }
    
    // Function definitions
    int add(int a, int b) {
        return a + b;
    }
    
    int subtract(int a, int b) {
        return a - b;
    }
    

    6. Function Overloading

    C++ supports function overloading, which allows you to define multiple functions with the same name but different parameter types or counts. This is useful for creating functions that can handle different data types.

    Example:

    double add(double a, double b) {
        return a + b; // For double types
    }
    
    int add(int a, int b) {
        return a + b; // For integer types
    }
    

    7. Default Arguments

    C++ also allows you to specify default arguments for function parameters. If an argument is not provided during the function call, the default value is used.

    Example:

    int add(int a, int b = 5) {
        return a + b; // b defaults to 5 if not provided
    }
    
    int main() {
        cout << "Sum with default: " << add(10) << endl; // Uses default value for b
        cout << "Sum without default: " << add(10, 20) << endl; // Uses provided value
        return 0;
    }
    

    Summary

    Understanding function definition and calling in C++ is essential for writing organized and efficient code. Functions allow you to encapsulate logic, promote code reuse, and simplify debugging. By using function declarations, definitions, and various features like overloading and default arguments, you can create flexible and powerful programs.

    Previous topic 13
    Introduction to Modular Programming
    Next topic 15
    Stack Rolling and Unrolling

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time4 min
      Word count632
      Code examples0
      DifficultyBeginner