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

9 Functions

The document explains the concept of functions in C++, defining them as reusable blocks of code that perform specific tasks and can reduce redundancy, enhance modularity, and provide abstraction. It outlines the advantages of using functions, such as easier coding, modification, maintenance, debugging, and time savings. Additionally, it details the syntax for declaring and defining functions, including examples of different types of functions based on parameters and return types.

Uploaded by

bihandu.com
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

9 Functions

The document explains the concept of functions in C++, defining them as reusable blocks of code that perform specific tasks and can reduce redundancy, enhance modularity, and provide abstraction. It outlines the advantages of using functions, such as easier coding, modification, maintenance, debugging, and time savings. Additionally, it details the syntax for declaring and defining functions, including examples of different types of functions based on parameters and return types.

Uploaded by

bihandu.com
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Functions

in C++
Function?
● Also known as procedure or subroutine in other programming
languages.
● A function is a group of statements that together perform a task.
● A function can be called many times.
● A function is a block of code that only runs when it is called.
Why do we need function?
● Reduce code redundancy.
○ If functionality is performed at multiple places in software, then rather than
writing the same code, again and again, it can create a function and call it
everywhere.

● Make code modular.


○ It becomes really simple to read and use the code if the code is divided into
functions
.
● Provide abstraction.
○ Can use library functions without knowing about their internal work.
Advantages of Functions
● Easier to Code
○ A lengthy program can be divided into small functions.
○ A function is written for a specific task.
○ A programmer can focus the attention on a specific problem.
● Easier to Modify
○ If there is an error in the program, it is corrected in the corresponding
function.
● Easier to Maintain & Debug
○ Each function contains independent source code.
○ A change in one part of the code does not affect other parts.
○ In case of an error, the infected function is debugged only.
○ The user does not need to examine the whole program.

● Reusability
○ They can be executed many times, can call a function whenever it needs.
○ It can be executed in different parts of the program to display the line
repeatedly.
● Less Programming Time
○ A program may be made up of many functions, which are written as
independent programs.
○ Different programmers can work on different functions simultaneously,
which saves a lot of time in the long run.
Functions

User-defined AKA Tailor-made


Library AKA Built-in Created by the programmer.
Pre-defined in C++.
The functions which are declared in the
C++ header files such as ceil(x), cos(x),
exp(x) etc.
Declaration of a function
● Syntax:

return_type function_name (parameter1, parameter2,...)


{
//code to be executed
}
● return_type : Data type specifier of the data returned by the function.
● function_name : The identifier by which it will be possible to call the
function.
● Parameters :
○ Each parameter consists of a data type specifier followed by an
identifier. (for example: int age)
● Allow passing arguments to the function when it is called.
● Different parameters are separated by commas.
● Function's body contains statement/s. It is a block of statements
surrounded by braces { }.
Functions

Built-in
User-defined

No return type - No parameters

With return type - no parameter/s.

With parameter/s - no return type.

With parameter/s and return type.


Syntax:
No Parameter/s - No Return Type
void functionName (){
//function implementation
}

Example:
void addition(){
int total = 0;
int x = 10;
int y = 20;

total = x + y;

cout << “Total is : ” << total;


}
Syntax: With Return Type - No Parameter
returnType functionName (){
//function implementation
//return statement
}

Example:
int addition(){
int total = 0;
int x = 10;
int y = 20;

total = x + y;

return total;
}
With Parameter/s - No Return Type
Syntax:
void functionName (dataType parameter1,
dataType parameter2){
//function implementation
}

Example:
void addition(int x, int y){
int total = 0;

total = x + y;

cout << “Total is : ” << total;


}
With Parameter/s and Return Type
Syntax:
returnType functionName (dataType
parameter1, dataType parameter2){
//function implementation
//return statement
}

Example:
int addition(int x, int y){
int total = 0;
total = x + y;

return total;
}
Calling the Function

● Calling or invoking the function locates the function in the memory,


furnishing it with arguments and causing it to execute.
● When a function is called then the control passed to the function where it is
actually defined.
● The actually statements are executed, and control passed again to the
calling program.

Syntax:
variable= function_name(argument1, argument1, …, argumentN);
Prototyping a Function

● While writing a program, function cannot be used without specifying its type or
without telling the compiler about it.
● Therefore, before calling a function, it must be either declared or defined.
● Thus, declaring a function before calling a function is called function
declaration or prototype which tells the compiler that at some point of the
program we will use the function of the name specified in the prototype.

Syntax:
returnType functionName(dataType, dataType, … );
OR
returnType functionName(dataType parameter1, dataType parameter2, … );
Thank You !!!

You might also like