0% found this document useful (0 votes)
4 views7 pages

Function

The document explains C++ functions, detailing their purpose, syntax, and examples of usage. It covers parameters, call by value and call by reference, and includes examples demonstrating these concepts. Additionally, it describes class methods and how to define them both inside and outside of a class definition.

Uploaded by

rohitgaikwad6157
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Function

The document explains C++ functions, detailing their purpose, syntax, and examples of usage. It covers parameters, call by value and call by reference, and includes examples demonstrating these concepts. Additionally, it describes class methods and how to define them both inside and outside of a class definition.

Uploaded by

rohitgaikwad6157
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

C++ Functions

 A function is a block of code which only runs when it is called.

 You can pass data, known as parameters, into a function.

 Functions are used to perform certain actions, and they are


important for reusing code: Define the code once, and use it many
times.

Syntax:

Data_type function_name(){
//block of code
}

Example:

void myFunction() {

// code to be executed

 myFunction() is the name of the function.


 void means that the function does not have a return value.

Example:
#include <iostream>
using namespace std;

void myFunction() {
cout << "object oriented programming\n";
}

int main() {
myFunction();
myFunction();
myFunction();
return 0;
}

Output:
object oriented programming
object oriented programming
object oriented programming

Parameters and Arguments


 Information can be passed to functions as a parameter.
 Parameters are specified after the function name, inside the
parentheses. You can add as many parameters as you want, just
separate them with a comma:

Syntax:
DataType FunctionName(parameter1,parameter2,parameter3){
//code to be executed
}

Example:

Void myFunction(int a,int b){

int c = a+b;
}

 Void is return type,tat means it doesn’t return any value.


 myFunction is the name of the function.
 int a,int b are the parameters.
Example:

#include <iostream>
#include <string>
using namespace std;

void myFunction(string name) {


cout << "Hello, "<< name << endl;
}

int main() {
myFunction("Sachin");
myFunction("virat");
myFunction("kapil");
return 0;
}

Output:

Hello, Sachin
Hello, virat
Hello, kapil

Call by value and call by reference


 There are two ways to pass value or data to function in C++ language. call by
value and call by reference.
 Original value is not modified in call by value but it is modified in call by
reference.

Call by value in C++


 In call by value, original value is not modified.
 In call by value, value being passed to the function is locally stored by the
function parameter in stack memory location.
 If you change the value of function parameter, it is changed for the current
function only. It will not change the value of variable inside the caller method
such as main().
Example:

#include <iostream>
using namespace std;

void swap(int a,int b){


int temp=a;
a=b;
b=temp;
}

int main() {

int a=10;
int b=20;
cout<<"before swap "<<”a =”<<a<<"b="<<b<<endl;
swap(a,b);

cout<<"after swap "<<”a =”<<a<<"b="<<b;


return 0;

Output:

before swap a=10 b=20


after swap a=10 b=20

Call by reference in C++


 In call by reference, original value is modified because we pass reference
(address).
 Here, address of the value is passed in the function, so actual and formal
arguments share the same address space.
 Hence, value changed inside the function, is reflected inside as well as outside
the function.
Example:

#include <iostream>
using namespace std;
void swap(int*a,int*b){
int temp =*a;
*a=*b;
*b=temp;
}
int main() {
int a=10;
int b=20;
cout<<"before swap "<<”a =”<<a<<"b="<<b<<endl;
swap(&a,&b);
cout<<"before swap "<<”a =”<<a<<"b="<<b;
return 0;
}

Ouput:
before swap a=10 b=20
after swap a=20 b=10

Symbol Name Description

& (ampersand sign) Address operator Determine the address of a variable.

∗ (asterisk sign) Indirection operator Access the value of an address.


Class Methods
Methods are functions that belongs to the class.

There are two ways to define functions that belongs to a class:

1. Inside class definition

#include <iostream>
using namespace std;

class MyClass {
public:
void myMethod() {
cout << "Hello World!";
}
};

int main() {
MyClass myObj;
myObj.myMethod();
return 0;
}

Output:

Hello Wrold!

2. Outside class definition

 To define a function outside the class definition, you have to declare it


inside the class and then define it outside of the class.

 This is done by specifiying the name of the class, followed the scope
resolution :: operator, followed by the name of the function.

returnType className :: methodName(){


//block of code
}

Example:
#include <iostream>
using namespace std;

class MyClass {
public:
void myMethod();
};

void MyClass::myMethod() {
cout << "Hello World!";
}

int main() {
MyClass myObj;
myObj.myMethod();
return 0;
}

Output:

Hello world!

You might also like