You are on page 1of 19

Lecture 6

Functions
The Concept of Functions
Functions are the basic building blocks
of C++ programs
A function is a block of statements with
a given name, which can be treated as
a unit.
There are two main types of functions
in C++
Standard C++ library functions
Programmer-defined (user-defined)
functions
Anatomy of a Function
A function consists of six parts:
Return type
Function name Signature of the function

Argument list
Opening brace (start of function)
Function body
Closing brace (end of function)
Function Declaration
According to ANSI standard, the declaration of a
function specifies the interpretation and attributes of
the function

The function declaration is a single statement which


specifies the return type, function name, and
argument list.

The syntax for a function declaration is:


return_type function_name(argument
list);
Function Definition
The definition of a function requires the C++
compiler to reserve storage for the function.

It defines what the function does, as well as gives


the number and type of arguments passed to the
function.

The syntax for a function definition is:


return_type function_name(argument list)
{
statement(s); /* Function body */
}
Example 1: Function to Add
Numbers

Function declaration
int addnumbers(int x, int y);

Function definition
int addnumbers(int x, int y){
int ans;
ans = x + y;
return ans;
}
Example 2: Square of a Number

Function declaration
int square(int x);

Function definition
int square(int x){
int ans;
ans = x * x;
return ans;
}
#include<iostream>
using namespace std;

/* Function declaration */
int addnumbers(int x, int y);

int main(){
int result;
result=addnumbers(5,12); /*Function call*/
cout<<result<<endl;
system("PAUSE");
return 0;
}

/* Function definition */
int addnumbers(int x, int y){
int ans;
ans=x+y;
return ans;
}
Return Types
A function can be declared to return any data type.

The return statement used in a function definition


returns a single value whose type should match the
one in the function declaration.

By default, the return type of a function is int, if no


explicit data type is specified.

e.g. addnumbers(int x, int y){


return x+y;
}
Void Functions
A function of type void has a return statement
without any specified value. i.e. return;

This may seem useless, but in practice void is used


often.

A good example is when a methods only purpose is


to print to the screen.

If no return statement is used in a method of type


void, it automatically returns at the end
Example: Void Function
void sayhi(char name[]){
cout<<"Hi <<name<<endl;
return;
}

int main(){
char name[16];
cout<<"Enter your name: ";
cin>>name;
sayhi(name);
system("PAUSE");
return 0;
}
Making Function Calls
A function call is an expression that can be used as
a single statement or within other statements.

When a function call is made, the program execution


jumps to the function and finishes the task assigned
to the function.

The program then resumes after the called function


returns.
Making Function Calls (contd)
To call a function, specify the name of the function followed by
a list of comma separated arguments in parentheses:
e.g. sayhi(George);

If the function returns a value then obtain the value and assign
it to a variable.
e.g. sum=addnumbers(5, 10);
cout<<addnumbers(5, 10)<<endl;

If the function has no arguments, you still need to follow the


function name with empty parentheses:
e.g. size();
Blocks and Local Scope

A block is a list of statements within curly braces

Blocks can be put anywhere a statement can be put

Blocks within blocks are nested blocks

An object name is known only within the block in which


it is defined and in nested blocks of that block

A parameter can be considered to be defined at the


beginning of the block corresponding to the function
body
Global Scope
Objects not defined within a block are global
objects
A global object can be used by any function in the file that is
defined after the global object
It is best to avoid programmer-defined global objects
Exceptions tend to be important constants

Global objects with appropriate declarations can even be


used in other program files
cout, cin, and cerr are global objects that are defined in
by the iostream library

Local objects can reuse a global object's name


Unary scope operator :: can provide access to global object
even if name reuse has occurred
Function Overloading
A function name can be overloaded
A Function with the same name but with different
interfaces is said to be overloaded
Typically this means different formal parameter lists
Difference in number of parameters
Min(a, b, c)
Min(a, b)
Difference in types of parameters
Min(10, 20)
Min(4.4, 9.2)
Function Overloading
int Min(int a, int b) {
cout << "Using int min()" << endl;
if (a > b)
return b;
else
return a;
}
double Min(double a, double b) {
cout << "Using double min()" << endl;
if (a > b)
return b;
else
return a;
}
Function Overloading
int main() {
int a = 10;
int b = 20;
double x = 4.4;
double y = 9.2;
int c = Min(a, b);
cout << "c is " << c << endl;
int z = Min(x, y);
cout << "z is " << z << endl;
Function Overloading
Compiler uses function overload resolution to call the
most appropriate function
First looks for a function definition where the formal and
actual parameters exactly match
If there is no exact match, the compiler will attempt to cast
the actual parameters to ones used by an appropriate
function

The rules for function definition overloading are very


complicated
Advice
Be very careful when using this feature

You might also like