Cpp - Function Function Parameters

Introduction

A function receives data via function parameters.

There can be more than one parameter as long as they are separated by commas.

A function can be called with no parameters at all.

The parameters passed to a function are local variables within that function.

Consider the following sample code, which appears to swap the values of two variables:

int x = 4, y = 13; 
swap(x, y); 

void swap(int x, int y) { 
    int temp = x; 
    x = y; 
    y = temp; 
} 

The swap() function failed to swap the variable values so that x equals 13 and y equals 4.

The parameters received by the swap() function are local variables within that function.

Pass by value

Changes made to function parameters do not affect the values in the calling function.

This is called passing by value because values are passed to the function and a local copy is made of each parameter.

Related Topics

Exercise