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

C++ Function

Functions allow programmers to organize code into reusable modules. Functions can be called multiple times and make code more modular, reusable, and easier to debug. Functions perform a specific task and are defined with a name, parameters, and body. Functions can take parameters, return values, and be passed by value or reference. Overloaded functions have the same name but different parameters. Functions improve code organization and maintainability.

Uploaded by

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

C++ Function

Functions allow programmers to organize code into reusable modules. Functions can be called multiple times and make code more modular, reusable, and easier to debug. Functions perform a specific task and are defined with a name, parameters, and body. Functions can take parameters, return values, and be passed by value or reference. Overloaded functions have the same name but different parameters. Functions improve code organization and maintainability.

Uploaded by

on.bonimos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

CHAPTER 5

▪ Function are used for divide a large code into module, due
to this we can easily debug and maintain the code.
▪ E.g. if we write a calculator programs at that time we can
write every logic in a separate function [For addition sum(),
for subtraction sub()].
▪ Any function can be called many times.

▪ Code Re-usability

▪ Develop an application in module format.

▪ Easily to debug the program.

▪ Code optimization: No need to write lot of code.

Computer Programming 3/20/2021 2


▪ A function is a group of statements that together perform
a specific task.
▪ Every C++ program has at least one function, which is main().

▪ Every function that is called in a program must be


defined somewhere (once only).
▪ The syntax is:
return_type function_name(parameter)
{
function body;
}

Computer Programming 3/20/2021 3


▪ A function declaration is the process of tells the compiler
about a function name.
▪ Then, the actual body of the function can be defined
separately.
▪ The syntax is: return_type function_name(parameter);

Computer Programming 3/20/2021 4


▪ The syntax is: function_name(); // or
variable=function_name(argument);

Computer Programming 3/20/2021 5


sum() Print out:

#include<iostream.h> Sum: 33

void sum(); // declaring a function


int a = 11, b = 22, c;

void main()
{
sum(); // calling the function
}

void sum() // defining the function


{
c = a + b;
cout << "Sum: " << c;
Computer Programming
} 3/20/2021 6
▪ There are two type of function in C++ Language. They are:
▪ Library function or pre-define function.
▪ User defined function.

Computer Programming 3/20/2021 7


▪ Library functions are the built-in function in C++ programming.

▪ Programmer can use library function by invoking function


directly; they don't need to write it themselves, for example:
#include <iostream>
#include <cmath> // includes the content of cmath file.
using namespace std;
int main() Print out:
{ Enter a number: 26
double number, squareRoot;
Square root of 26 = 5.09902
cout << "Enter a number: ";
cin >> number;
// sqrt() is a library function to calculate square root.
squareRoot = sqrt(number);
cout << "Square root of " << number << " = " << squareRoot;
return 0;
}
Computer Programming 3/20/2021 8
▪ If a function take any arguments, it must declare variables
that accept the values as a arguments.
▪ These variables are called the formal parameters of the
function.

Computer Programming 3/20/2021 9


▪ There are two ways to pass value or data to function.
▪ call by value
▪ call by reference

▪ Call by value: In call by value, original value can not


be changed or modified.
▪ Call by reference: In call by reference, original value
is changed or modified because we pass a reference
(an address). 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.

Computer Programming 3/20/2021 10


The value of num1 and num2 are initialized to variables a and b respectively.
These arguments a and b are called formal arguments. For example:

Computer Programming 3/20/2021 11


▪ A function can return a single value to the calling program
using return statement. In the previous program, the value
of add is returned from user-defined function to the calling
program using statement below:

Computer Programming 3/20/2021 12


Call by Value
#include<iostream.h>

void swap(int a, int b){


int temp;
temp = a; Print out:
a = b; Value of a: 111
b = temp; Value of b: 222
}

void main(){
int a=111, b=222;
swap(a, b); // passing value to function
cout<<"Value of a: "<< a <<endl;
cout<<"Value of b: "<< b <<endl;
}
Computer Programming 3/20/2021 13
Call by Reference
#include<iostream.h>

void swap(int *a, int *b){


int temp;
temp = *a; Print out:
*a = *b; Value of a: 222
*b = temp; Value of b: 111
}

void main(){
int a=111, b=222;
swap(&a, &b); // passing value to function
cout<<"Value of a: "<< a <<endl;
cout<<"Value of b: "<< b <<endl;
}
Computer Programming 3/20/2021 14
▪ Arguments passed by value and by reference.
For example:

When f() is called, val++ For example: int main(){


increments a local copy of int i=1;
the first actual argument, int j=1;
whereas ref++ increments f ( i , j );
the second actual argument. }
➢ It will increment j but not i.
Computer Programming 3/20/2021 15
Difference Between Call by Value and Call by Reference.
call by Value call by Reference
This method copy address of
This method copy original value
arguments into function as a
into function as a arguments.
arguments.
Changes made to the parameter
Changes made to the parameter
affect the argument. Because
inside the function have no effect
address is used to access the
on the argument.
actual argument.
Actual and formal arguments will Actual and formal arguments will
be created in different memory be created in same memory
location location

Note: By default, C++ uses call by value to pass arguments.

Computer Programming 3/20/2021 16


▪ A function can be defined inline.

▪ The inline directive can be included before a function


declaration to specify that the function to be compiled
as code in the same point where it is called.
Syntax: inline type name ( arguments ... ) {
instructions ... }
▪ It is not necessary to include the inline keyword in
the call, it is just like the one for any other function.
▪ For example: inline int factorial (int n)
{
return (n>2)? 1 : n* factorial(n-1);
}

Computer Programming 3/20/2021 17


Inline

#include<iostream.h>

inline void show() Print out:


{ Hello word
cout<<"Hello world";
}

void main()
{
show(); // Call it like a normal function
}

Computer Programming 3/20/2021 18


▪ Two or more functions having same name but different
argument(s) are known as overloaded functions.
▪ Here, all 4 functions are overloaded functions called “test”
because argument(s) passed to these functions are different.

▪ For example: int test() { }


int test(int a) { }
float test(double a) { }
int test(int a, double b) { }
▪ N.B., the return type of all these 4 functions are not same.
Overloaded functions may or may not have different return
type but it should have different argument(s).
double test(int a) { }
int test(int b) { } // ERROR CODE!!!
Computer Programming 3/20/2021 19
Passing string to a Function
#include <iostream> Print out:
using namespace std; string is: passing str1
void display(char *); char array is: passing str
void display(string);
int main(){
string str1 = "passing str1";
char str[] = "passing str";
display(str1);
display(str); void display(char s[]){
cout << "char array is: " << s << endl;
return 0; }
} void display(string s)
{
cout << "string is: " << s << endl;
}
Computer Programming 3/20/2021 20

You might also like