ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP1112Function Definition An…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP1112

    Programming Fundamentals

    Progress0 / 19 done
    Topics19
    1Introduction to Problem Solving2Von-Neumann Architecture3Introduction to Programming4Role of Compiler and Linker5Introduction to Algorithms6Basic Data Types and Variables7Input/Output Constructs8Arithmetic, Comparison and Logical Operators9Conditional Statements and Execution Flow10Repetitive Statements and Execution Flow11Lists and Memory Organization12Multi-dimensional Lists13Introduction to Modular Programming14Function Definition and Calling15Stack Rolling and Unrolling16Strings and String Operations17Pointers/References18Static and Dynamic Memory Allocation19File I/O Operations
    4 min read
    Topic 14 of 19
    Topic Study Note Illustration
    14

    Function Definition and Calling

    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...

    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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    632words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals