Class 1 Class 2 Class 3 Class 4 Class 5 Class 6 Class 7 Class 8

Functions in C++ for Beginners

Functions in C++ are one of the most important concepts in programming. A function is a block of code designed to perform a specific task. Functions help programmers divide large programs into smaller and manageable parts, making the code easier to read, debug, and reuse. C++ provides both built-in and user-defined functions that improve program efficiency and reduce code repetition.

Functions in C++

To make larger programs manageable, programmers divide them into smaller modules called functions. In a function the complex program is divided into simple subprograms. These functions are easy to write, easy to understand and easy to debug.

Advantages of Functions

  • Reduce program complexity.
  • Improve readability of code
  • Help in debugging and testing
  • Allow code reuse
  • Save programming time

Syntax:

return_type function_name(parameter_list)
{
    // function body
    statements;
}

Question: Write a program to print the message “welcome to my program”.

#include <iostream>
using namespace std;

// function declaration
void display();

int main() {

    // function call
    display();

    return 0;
}

// function definition
void display() {

    cout << "Welcome to my  program";
}

Function with parameters.

A function with arguments is a function that receives values from the calling function. These values are called arguments or parameters. Arguments allow functions to work with different data values. Functions with arguments make programs more flexible and reusable.

Syntax:

return_type function_name(parameter1, parameter2)
{
    // function body
}

Question: Write a program to find the sum of numbers using a function.

#include <iostream>
using namespace std;

// function declaration
void add(int a, int b);

int main() {
    int num1, num2;

    cout << "Enter two numbers: ";
    cin >> num1 >> num2;

    // function call
    add(num1, num2);

    return 0;
}

// function definition
void add(int a, int b) {
    int sum;

    sum = a + b;

    cout << "Sum = " << sum;
}

Default Arguments

Default arguments are the values assigned to function parameters at the time of function declaration. If the user does not pass values for those parameters during the function call, the default values are used automatically.

Syntax:

return_type function_name(parameter = value);

Question: Write a program to print “Hello student” using default parameters.

#include <iostream>
using namespace std;

// function with default argument
void greet(string name = "Student")
{
    cout << "Hello " << name;
}

int main()
{
    greet();          // uses default value
    return 0;
}

Pass by value and pass by reference

Functions can receive values in two ways:

  1. Pass by value
  2. Pass by reference

1. Pass by value

In pass-by-value, a copy of the actual value is passed to the function. Changes made inside the function do not affect the original variables.

Question: Find the sum of two numbers using pass by value.

#include <iostream>
using namespace std;

void sum(int a, int b)
{
    int s = a + b;
    cout << "Sum (Pass by Value) = " << s;
}

int main()
{
    int x = 10, y = 20;

    sum(x, y);

    return 0;
}

2. Pass by Reference

In pass by reference, the address/reference of the original variable is passed to the function. Changes made inside the function affect the original variable.

Question: Find the sum of two numbers using pass by reference.

#include <iostream>
using namespace std;

void sum(int &a, int &b)
{
    int s = a + b;
    cout << "Sum (Pass by Reference) = " << s;
}

int main()
{
    int x = 10, y = 20;

    sum(x, y);

    return 0;
}

Disclaimer: We have provide you with the accurate handout of “Functions in C++ for Beginners“. If you feel that there is any error or mistake, please contact me at anuraganand2017@gmail.com. The above study material present on our websites is for education purpose, not our copyrights.

Images and content shown above are the property of individual organisations and are used here for reference purposes only. To make it easy to understand, some of the content and images are generated by AI and cross-checked by the teachers.

cbseskilleducation.com

Leave a Comment