Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ Functions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Functions

Hassan Bashir
INTRODUCTION TO
FUNCTIONS
A function is a named block of code that perform some
action. The statement written in function are executed when
it is called by its name. Each function has a unique name.
Function are the building block of C++ programs. They are
used to perform the tasks that are repeated many times
Importance of Function
• A program may need to repeat the same
piece of code at various places.
• It may be required to perform certain task
repeatedly.
• The program may become very large if
functions are not used.
• The real reason for using function is to
divide program into different parts.
Advantages of Functions

• Easier to Code
• Easier to Modify
• Easier to Maintain
• Reusability
• Less Programming Time
• Easier to Understand
Types of Functions
1. Built – in Library Function
2. User Defined Function
Library Functions.
• Functions which are already defined, compiled and stored in
different header file of C Library are known as Library
Functions.
• These functions cannot be modified by user.
• Users do not know the internal working of these type of
functions.
• For Example: cout(), cin(), system("cls"), rand(), srand () etc.
system("cls")
• Use for clear screen in dev c++

Output
rand()
• The rand() function in C++ is used to generate random
numbers; it will generate the same number every time we
run the program.
rand()
#include<iostream>
#include <cstdlib> Header File
using namespace std;
int main() Output 1 Output 2
{
int Num;
for(int i = 0; i<5; i++){
Num = rand() ;
cout<<Num;
cout<<endl;}
}
srand()
• srand() function sets the initial point for generating the
pseudo-random numbers.
rand()
#include<iostream>
#include <cstdlib>
#include<time.h>
using namespace std;
int main()
{ OUTPUT 1 OUTPUT 2
int Num;
srand(time(0));
for(int i = 0; i<5; i++){
Num = rand() ;
cout<<Num;
cout<<endl;} }
rand()
#include<iostream> Five random number between 1 to 10
#include <cstdlib>
#include<time.h>
using namespace std;
int main()
{ Output 1 Output 2
int Num;
srand(time(0));
for(int i = 0; i<5; i++){
Num = rand() % 10 + 1 ;
cout<<Num;
cout<<endl;}
}
rand()
#include<iostream> Five random number between 5 to 15
#include <cstdlib>
#include<time.h>
using namespace std;
int main()
{ Output
int Num;
srand(time(0));
for(int i = 0; i<5; i++){
Num = 5 +(rand() % (15 - 5 + 1 ));
cout<<Num;
cout<<endl;}
}
rand()
#include<iostream> Five random characters between A to E
#include <cstdlib>
#include<time.h>
using namespace std;
int main()
{ Output
char ch;
srand(time(0));
for(int i = 0; i<5; i++){
ch = 65 +(rand() % (69-65));
cout<<ch;
cout<<endl;}
}
User-Defined Functions

C++ programs usually have the


following form:

// include statement
// function prototype
// main() function
// function definitions
Function Input and Output
Function Definition

A set of statements that explains what a function does is


called FUNCTION Definition
A function definition can be written at:
• Before main() function
• After main() function
• In a separate file
Syntax of Function Definition
Return-type Function-name (parameters) Function header

{
statement 1;
statement 2;
: Function body
:
:
statement N;
}
Function Declaration
Function declaration is the model of a function. It is
also known as FUNCTION PROTOTYPE. It
provides information to compiler about the structure
of function to be used in program. It ends with
semicolon (;). It consists of:
• FUNCTION NAME
• FUNCTION RETURN TYPE
• NUMBERS & TYPES OF PARAMETERS
Parameters
are variables to hold values of arguments passed while
function is called. A function may or may not contain
parameter list.
Syntax of Function Declaration
Return-type Function-name (parameters);

Indicates the type Indicates the Parameters are


of value that will name of the values that
be returned by function are provided to a
function function when the
function is called.
Example of Function Declaration &
Function Definition
Function Call
The statement that activates a function is
known as FUNCTION CALL.The following
steps
take place when a function is called:
1. The control moves to the function that is
called.
2. All statements in function body are executed.
3. Control returns back to calling function.
Function Call Mechanism
Called
Calling
Void main() function
function
{
……. Void Fun()
Fun(); {
Function ……. …..
calls …..
…….
Fun(); }
…….
}
Example of Function Call
Local Variable Global Variable

• Local variables are declares • Global variables are declares


within a function. outside any function
• It can be used only in function • Global variables can be used
in which they declared. in all function.
• Local variable are destroyed • Global variable are destroyed
when control leave the when the program is
function. terminated.
• Local variables are used when • Global variables are used
the vales are to be used within when values are to be shared
a function. among different functions.
Call by Value and Call by
Reference
Call by Value

• In call by value, original value can not be changed or


modified. In call by value, when you passed value to the
function it 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 but it
not change the value of variable inside the caller function
such as main().
Call by Value
#include<iostream>
void swap(int a, int b)
OUTPUT:
{
int temp; Value of a: 200
temp=a; Value of b: 100
a=b;
b=temp;
}
int main() {
int a=100, b=200;
swap(a, b); // passing value to function
cout<<"Value of a"<<a;
cout<<"Value of b"<<b;
}
Call by Reference
• call by reference, original value is changed or
modified because we pass reference (address). Here,
address of the value is passed in the function, Hence,
any value changed inside the function, is reflected
inside as well as outside the function.
Call by Reference
#include<iostream>
void swap(int *a, int *b)
{ Output:
int temp;
temp=*a; Value of a: 200
*a=*b; Value of b: 100
*b=temp;
}
int main() {
int a=100, b=200;
swap(&a, &b); // passing value to function
cout<<"Value of a"<<a;
cout<<"Value of b"<<b;
}
Call by Value Call by Reference
• Call by value passes the • Call by reference passes the
value of actual parameter to address of actual parameter
formal parameter. to formal parameter.
• Actual & formal parameter • Actual & formal parameter
refer to different memory refer to same memory
location. location.
• It requires more memory. • It requires less memory.
• It is less efficient. • It is more efficient.

You might also like