Functions Final
Functions Final
By
Irfan Abdullah
CONTENTS
• Explain the concept and types of functions • Use inline functions
• Explain the advantages of using functions • Pass the arguments
• Explain the signature of functions (Name, • by constants
Arguments, Return type) • by value
• by reference
• Explain Function prototype, Function
definition, Function call • Use default arguments
• Differentiate among local global and static • Use return statement
variable • Know advantage of function overloading
• Differentiate between formal and actual • Understand the use of function
parameters overloading with Number of arguments
• Know the concept of local and global • Data type of arguments
functions
• Return type
Functions
• A function is a group of statements that together perform a task.
• Every C++ programs has at least one function, which is main()
additional functions can be defined in the program. Each
function performs a specific task .
• Functions are one of the main building block of any
programming language. Functions are used to provide
modularity to a program. Creating an application using function
makes it easier to understand, edit, check errors etc. function
makes the work easier by writing the code once and executing it
again and again as many times as required.
Types of Functions
• C++ functions are categorized into the following two types
• Library or built in functions
• User defined functions
Library or Built in Functions
• Library or built in functions are the pre-defined functions in C++ .
• Programmers can use these functions by Invoking calling them
directly, they do not need to write code for them.
• C+ + provides a series of library or pre-defined functions for various
commonly used operations of algebra, geometry, trigonometry,
finance, graphics, clipboards etc. these functions are part of the C++
language and are completely reliable.
• Some examples of commonly used library or built in functions are:
• abs(), div(), getch(), pow(),getchar(), puts(),sqrt(), strcmp(),time()
Library or Built in Functions
// Program to find square root of a number using #include <iostream>
sqrt) built in function
#include<iostream> #include <math.h>
#include<math.h > using namespace std;
using namespace std; int main()
int main() {// Getting a double value
{ double x;
double number, squareroot; cout << "Please enter a real number: ";
cout <<“Enter a number:”; cin >> x;
cin>>number; // Compute the ceiling and the floor of the real number
//sqrt() is a library functions to calculate square root cout << "The ceil(" << x << ") = " << ceil(x) << endl;
squareroot = sqrt(number); cout << "The floor(" << x << ") = " << floor(x) << endl;
cout<<“square root of ”<< number<<“=“<< }
squareroot <<endl;
return 0;
}
User Defined Functions
• C++ allows programmers to define their own functions. The
functions defined by the programmers, according to their needs
are called users defined functions. If any function is not
available as built in function, users can define their own
functions and use them In the same way as built in functions are
used.
• For example, the <math.h> library does not include a standard
function that allows users to round a real number to the ith
digits, therefore, we must declare and implement this function
ourselves
Advantages of User Defined Function
• Single list of instructions within main() • Reducing Complexity of Program:
functions and such programs are known as Complex program can be decomposed into
monolithic program – i.e. program containing small sub-programs or user defined functions.
a large single list of instructions. These types of
programs are very difficult to understand, • Easy to Debug and Maintain : During
debug, test and maintain. So to avoid these debugging it is very easy to locate and isolate
difficulties we use user defined functions. faulty functions. It is also easy to maintain
program that uses user defined functions.
• Advantages of using user defined functions in
C programming are listed below: • Readability of Program : Since while using
user defined function, a complex problem is
• Reduction in Program Size: Since any divided in to different sub-programs with clear
sequence of statements which are repeatedly objective and interface which makes easy to
used in a program can be combined together to understand the logic behind the program.
form a user defined functions. And this
functions can be called as many times as • Code Reusability : Once user defined
required. This avoids writing of same code function is implemented it can be called or
again and again reducing program size. used as many times as required which reduces
code repeatability and increases code
reusability.
Defining a C++ Function
• Generally speaking, we define a C++ • A function must be defined first to use it
function in two steps (preferably but not in the program
mandatory) • A C++ function consists of two parts
1. Declare the function signature in either • The function header, and
a header file (.h file) or before the main • The function body
function of the program
• The general syntax for defining a user
2. Implement the function in either an defined function is
implementation file (.cpp) or after the return-type function-name (parameters)
main function
{
//function-body
}
• The function header has the following
syntax
return-type function-name (parameters)
• The function body is simply a C++ code
enclosed between { }
Function Declarations/Prototype
• A function declaration tells the compiler about a function name
and how to call the function. The actual body of the function can
be defined separately.
• A function declaration has the following parts
return_type function_name( parameter list );
• Example :
int max(int num1, int num2);
• Parameter names are not important in function declaration only
their type is required, so following is also valid declaration.
int max(int, int);
Defining a C++ Function
• The general form of a C++ function definition is as • Following is the source code for a function called max().
follows: This function takes two parameters num1 and num2 and
return_type function_name( parameter list ) return the biggest of both.
{ body of the function }
• A C++ function definition consists of a function header // function returning the max between two numbers
and a function body. Here are all the parts of a function.
• Return Type − A function may return a value. The int max(int num1, int num2) {
return_type is the data type of the value the function // local variable declaration
returns. Some functions perform the desired operations int result;
without returning a value. In this case, the return_type
is the keyword void. if (num1 > num2)
• Function Name − This is the actual name of the result = num1;
function. The function name and the parameter list else
together constitute the function signature. result = num2;
• Parameters − A parameter is like a placeholder. When
a function is invoked, you pass a value to the parameter. return result;
This value is referred to as actual parameter or }
argument. The parameter list refers to the type, order,
and number of the parameters of a function. Parameters
are optional; that is, a function may contain no
parameters.
• Function Body − The function body contains a
collection of statements that define what the function
does.
Calling a Function #include <iostream>
using namespace std;
// function declaration
• While creating a C++ function, you give a int max(int num1, int num2);
definition of what the function has to do. To int main () {
use a function, you will have to call or invoke // local variable declaration:
that function. int a = 100;
• When a program calls a function, program int b = 200;
control is transferred to the called function. A int ret;
called function performs defined task and // calling a function to get max value.
when it’s return statement is executed or when ret = max(a, b);
its function-ending closing brace is reached, it cout << "Max value is : " << ret << endl;
returns program control back to the main
program. return 0;
}
• To call a function, you simply need to pass the // function returning the max between two numbers
required parameters along with function
name, and if function returns a value, then you int max(int num1, int num2) {
can store returned value. // local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Types of User-defined Functions in C++
• There can be 4 different types
of user-defined functions,
they are:
1. Function with no arguments
and no return value
2. Function with no arguments
but a return value
3. Function with arguments but
no return value
4. Function with arguments and
a return value
Function with no argument and no return
value
• When a function has no arguments, it does not receive any data from
the calling function. Similarly when it does not return a value, the
calling function does not receive any data from the called function.
• Syntax for function is: