C++ Functions
C++ Functions
C++ 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
// include statement
// function prototype
// main() function
// function definitions
Function Input and Output
Function Definition
{
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);