Function Definition

Pass by value is used where the value of x is not modified using the function fun().

C++




// C++ Program to demonstrate function definition
#include <iostream>
using namespace std;
 
void fun(int x)
{
    // definition of
    // function
    x = 30;
}
 
int main()
{
    int x = 20;
    fun(x);
    cout << "x = " << x;
    return 0;
}


Output

x = 20

Time complexity: O(1)

Space complexity: O(1)

Functions in C++

A function is a set of statements that takes input, does some specific computation, and produces output. The idea is to put some commonly or repeatedly done tasks together to make a function so that instead of writing the same code again and again for different inputs, we can call this function.
In simple terms, a function is a block of code that runs only when it is called.

Syntax:

Syntax of Function

Example:

C++




// C++ Program to demonstrate working of a function
#include <iostream>
using namespace std;
 
// Following function that takes two parameters 'x' and 'y'
// as input and returns max of two input numbers
int max(int x, int y)
{
    if (x > y)
        return x;
    else
        return y;
}
 
// main function that doesn't receive any parameter and
// returns integer
int main()
{
    int a = 10, b = 20;
 
    // Calling above function to find max of 'a' and 'b'
    int m = max(a, b);
 
    cout << "m is " << m;
    return 0;
}


Output

m is 20

Time complexity: O(1)

Space complexity: O(1)

Similar Reads

Why Do We Need Functions?

...

Function Declaration

Functions help us in reducing code redundancy. If functionality is performed at multiple places in software, then rather than writing the same code, again and again, we create a function and call it everywhere. This also helps in maintenance as we have to make changes in only one place if we make changes to the functionality in future. Functions make code modular. Consider a big file having many lines of code. It becomes really simple to read and use the code,  if the code is divided into functions. Functions provide abstraction. For example, we can use library functions without worrying about their internal work....

Types of Functions

A function declaration tells the compiler about the number of parameters, data types of parameters, and returns type of function. Writing parameter names in the function declaration is optional but it is necessary to put them in the definition. Below is an example of function declarations. (parameter names are not present in the below declarations)...

Parameter Passing to Functions

...

Function Definition

Types of Function in C++...

Functions Using Pointers

The parameters passed to the function are called actual parameters. For example, in the program below, 5 and 10 are actual parameters. The parameters received by the function are called formal parameters. For example, in the above program x and y are formal parameters....

Difference between call by value and call by reference in C++

Pass by value is used where the value of x is not modified using the function fun()....

Points to Remember About Functions in C++

...

Main Function

The function fun() expects a pointer ptr to an integer (or an address of an integer). It modifies the value at the address ptr. The dereference operator * is used to access the value at an address. In the statement ‘*ptr = 30’, the value at address ptr is changed to 30. The address operator & is used to get the address of a variable of any data type. In the function call statement ‘fun(&x)’, the address of x is passed so that x can be modified using its address....

C++ Recursion

...

C++ Passing Array to Function

Call by value Call by reference A copy of the value is passed to the function An address of value is passed to the function Changes made inside the function are not reflected on other functions Changes made inside the function are reflected outside the function as well Actual and formal arguments will be created at different memory location Actual and formal arguments will be created at same memory location....

C++ Overloading (Function)

1. Most C++ program has a function called main() that is called by the operating system when a user runs the program....

Friend Function

...