0% found this document useful (0 votes)
32 views

M1 Guide

Functions are reusable blocks of code that perform tasks. They improve modularity by breaking programs into smaller, specialized units. Functions have a prototype that specifies their name, return type, and parameters. They also have a definition that contains the implementation code. Functions can take arguments, which are passed either by value where the function gets a copy or by reference where changes to the argument are reflected in the original variable. Function overloading allows multiple functions to have the same name but different parameters.

Uploaded by

syhxns10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

M1 Guide

Functions are reusable blocks of code that perform tasks. They improve modularity by breaking programs into smaller, specialized units. Functions have a prototype that specifies their name, return type, and parameters. They also have a definition that contains the implementation code. Functions can take arguments, which are passed either by value where the function gets a copy or by reference where changes to the argument are reflected in the original variable. Function overloading allows multiple functions to have the same name but different parameters.

Uploaded by

syhxns10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Functions

• Useful analysis/computational units.


• Object and nonobject functions:
o cout.setw()
o ShowHeader()
o pow()
• Functions improve modularity of programs.
• Functions normally have two parts:
o Prototype (sometimes called interface)
o Definition (sometimes called implementation)

General Function Layout


Syntax:
return type name (<args> )
{
function body
}

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.

Example: Square and Cube of 1 through 5


#include <iomanip>
#include <cmath>
using namespace std;
// Prototype
void ShowPowers();
int main()
{
ShowPowers();
return 0;
}
/* ShowPowers()
*
* Show the square and cube of 1 through 5.
*/
void ShowPowers()
{
for( int i = 1 ; i <= 5 ; i++ )
{
cout << setw(6) << i ;
cout << setw(6) << i*i;
cout << setw(6) << i*i*i << endl;
}
}

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;}

You might also like