M1 Guide
M1 Guide
return type void, basic data types (int, double, etc.), or user/system defined types.
name Any valid name. The names and types of all arguments (zero or more), separated by
commas.
function body Zero or more valid statements.
Function Operation
When a function is invoked (called), the flow of control is transferred to the function. This means
that the next statement to be executed is the first one in the function that was called. After the
function completes its task the flow of control returns to the statement in the function that
invoked the function.
Function Prototypes
The prototype specifies the data type, name, and input (and/or output) parameter(s) of a
function.
• The type of a function is the data type of a value returned by the function.
• The name of a function is any valid identifier. Use action words (verbs) whenever possible
in naming functions.
• Parameters define input (and/or output) to a function.
• Examples:
o double sin(double);
o double Average (double x1, double x2, double x3);
Note: The function parameters are separated by commas
Function Definition
A function definition is a sequence of statements.
Function names should imply the operation(s) performed by the function.
All variables must be declared.
If the function does not return a value, the function return type must be declared as void.
Function Arguments
If a function takes any arguments, it must declare variables that accept the values as an
argument. These variables are called the formal parameters of the function. There are two ways
to pass value or data to function in C++ language which is given below;
• call by value
• call by reference
Call by value
In call by value, original value cannot be changed or modified. In call by value, when you passed
value to the function it is locally stored by the function parameter in stack memory location. If
you change the value of function parameter, it is changed for the current function only but it not
changes the value of variable inside the caller function such as main().
Example:
#include <iostream>
using namespace std;
void swap(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
}
int main()
{
int a=100, b=200;
swap(a, b); // passing value to function
cout<<"Value of a "<<a << endl;
cout<<"Value of b "<<b << endl;
return 0;
}
Call by reference
In call by reference, original value is changed or modified because we pass reference (address).
Here, address of the value is passed in the function, so actual and formal arguments shares the
same address space. Hence, any value changed inside the function, is reflected inside as well as
outside the function.
Example:
#include <iostream>
using namespace std;
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int main()
{
int a=100, b=200;
swap(&a, &b); // passing value to function
cout<<"Value of a "<<a << endl;
cout<<"Value of b "<<b << endl;
return 0;
}
Function Overloading
Function overloading is a feature in C++ where two or more functions can have the same name
but different parameters.
Example:
#include <iostream>
using namespace std;
int plusFuncInt(int x, int y) { return x + y;}
double plusFuncDouble(double x, double y) { return x + y;}
int main() {
int myNum1 = plusFuncInt(8, 5);
double myNum2 = plusFuncDouble(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;}