Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

FUNCTIONS

FUNCTIONS INTRODUCTION

 A function is a subprogram that acts on data and often returns a value.


 Data is passed from one function to another by arguments.
 When no arguments are used ,the function names are followed by” ()”.
 Functions are used to perform certain actions, and they are important
for reusing code: Define the code once, and use it many times.
ADVANTAGES

 Support for modular programming.


 Reduction in program size.
 Code duplication is avoided.
 Code reusability is provided.
 Functions can be called repetitively.
 A set of functions can be used to form libraries .
FUNCTIONS
FUNCTION IS DIVIDED INTO THREE
SECTIONS:

 Function prototype
 Function definition
 Function calling
FUNCTION PROTOTYPE

• A function prototype is simply the declaration of a function that


specifies function’s name, parameters and return type.
• Always terminated by semicolon.
The general form of function declaration :-
Return_type function_name (parameter list/argument);
FUNCTION DEFINITION
• A function definition provides the actual body of the function.
• A C ++ function definition consists of a function header and a function body .
Syntax :
return_type function_name(parameter list/argument)
Ex:
return_type function_name(parameter list/argument) Function header
{
Local variable declaration;
function body statement; Function Body
return statement;
}
FUNCTION CALLING

• A function must be called by its name followed by argument list


enclosed in semicolon.
Syntax:
function_name(parameter list/argument);
Note: data type not to be mentioned.
Ex: int main()
{
function_name(parameter list);
}
PARTS OF A FUNCTION
PASSING ARGUMENTS

 Call by Value(Pass by value)


 Call by Reference (Pass by address)
CALL BY VALUE

The call by value method copies the value of actual parameters into
formal parameters i.e. the function creates its own copy of arguments
and uses them.
. Call by Reference
In call by reference method in place of calling a value to the function
being called , a reference to the original variable is passed .i.e. the same
variable value can be accessed by any of the two names.
FUNCTION CATEGORIES
i) Function with no return value and no argument.
ii) Function with no arguments but returns a value.
iii) Function with arguments passed and no return value.
iv) Function with arguments and returns a value.
DEFAULT ARGUMENTS IN C++

 The default arguments are used when provide no arguments or only few arguments while calling a
function.
 The default arguments are used during compilation of program.
 In function prototype declaration , the default values are given.
 Ex: void sum(int x=10, int y=20)
RULES OF DEFAULT ARGUMENTS

 If assign default value to an argument, the subsequent arguments must have default values
assigned to them, else you will get compilation error.
 Valid: Following function declaration are valid –
int sum(int a=10 ,int b=20 ,int c=30);
int sum(int a ,int b=20 ,int c=30);
int sum(int a ,int b ,int c=30);
 invalid: Following function declaration are invalid –
int sum(int a=10 ,int b ,int c=30);
int sum(int a ,int b=20 ,int c);
int sum(int a=10 ,int b=20 ,int c)
RECURSION FUNCTION
 A programming technique in which a function calls itself is known as recursion.
 A recursive function is a function that calls itself during its execution.
The figure below shows how recursion works by calling the recursive function again and again.
FUNCTION OVERLOADING

 Function Overloading is defined as the process of having two or more function with the same name,
but different in parameters is known as function overloading in C++.
 The advantage of Function overloading is that it increases the readability of the program because you
don't need to use different names for the same action.
 Rules of function overloading:
Valid case overloading
 Functions have different parameter type:
 Function have different number of parameters:
 Function have different sequence of parameters:
Invalid case overloading
INTRODUCTION OF ARRAYS
 An array is a container object that holds a fixed number of values of a single type.
 The length of an array is established when the array is created.
 After creation, its length is fixed.
 Each item in an array is called an element, and each element is accessed by its numerical index.
 The syntax for an array declaration is
type arrayName [array Size];
 Syntax to access array elements;
array[index];

 Initializing Arrays
type array[5]={19,10,8,17,9,15};
MULTIDIMENSIONAL ARRAYS

 The simplest form of the multidimensional array is the two-dimensional array.


Syntax :
type name [size1] [size2]…. [size N];
Two-dimensional Array:
Syntax: Declaration
type arrayName [x][y];
 Initializing Two –Dimensional Arrays:
Multi -dimensioned arrays may be initialized by specifying bracketed values for each row.
int a[2][3] = { {6,8,9} ,{ 5,3,2}};
Three –Dimensional Arrays:
A three-dimensional array is an array of arrays.
Syntax:
data type arrayName [size 1][size 2] [size 3]
Int array_3D [4][5][6]= 24 elements
PASSING ARRAYS TO FUNCTIONS
 The syntax for passing an array to a function is:
returnType functionName (datatype arrayName[ arraySize])
{
//code
}
STRING
 A string is a sequence of character terminated with a null character ‘\o’.
 String literals are enclosed in double quotation marks.
 The difference between string literals and char literals.
STANDARD LIBRARY STRING FUNCTIONS

 strcpy()
The strcpy() function copies the string pointed by source (including the null character) to the
destination.
 strcat() : String Concatenation
The strcat() function will append a copy of the source string to the end of destination string.

You might also like