Faculty of Computer science
Object Oriented Programming Language
Lecturer: AbdulBasir Momand
OOP Chapter 01
Chapter 1
Introduction to Function
Learning Outcomes Chapter 01
• What is function
• Types of function
• Build-in function
• Arithmetic functions
• String functions
• User defined function
• Function declaration
• Function definition
• Function call
What is function?
A function is block of code which is used to perform a
particular task.
Types of function Chapter 01
• We have two types of function in C++:
function
Build-in function
User defined function
1. Build-in functions Chapter 01
What is Security?
Definition:
A function which has been created already and programmers are able
to use in their own programs.
Built-in functions are also known as library functions.
We need not to declare and define these functions as they are
already written in the C++ libraries such as sqrt(), strlen(), pow(), and
etc.
We can directly call Build-in functions, when we need.
1. Build-in Functions Chapter 01
Built-in functions can be further categories into two parts:
1. Arithmetic Library Functions
2. String Library Functions
1.1 Arithmetic Library Functions Chapter 01
• These are the functions used to perform operations with numeric
data.
• The <math.h> header file must be included before using the
arithmetic functions.
• Some examples are pow(x , y), sqrt(x) and etc.
1.1 Arithmetic Library Functions
Method Description
sqrt(x) Return square root value of a number
pow(x, y) It computes x raised to the power y.
round(x) It rounds off the value of x.
max(x, y) It returns the larger number among two numbers x and y.
min(x, y) It returns the smaller number among two numbers x and y.
abs(x) It computes the absolute value of x.
1.2 String Library Function Chapter 01
• String functions are those functions that perform operation on string
data.
• String data is nothing more just a group of characters enclosed in double
quotes.
• The <string.h> header file must be included in the program in order to
use these function.
1.2 String Library Function
Method Description
Strlen() Calculates the length of a string. It returns the number of
characters in a string.
strlwr(x) Converts the uppercase letters of a string to lowercase.
strupr(y) strupr() converts the lowercase letters of a string to uppercase.
Strcpy(x, y) It copies one string to another.
Strcat(x, y) Appends a copy of the source string to the destination string.
2. User Defined Functions Chapter 01
A function which created by the programmer for his own purpose is
called as User Defined Function.
To create your own function you must go through the following
steps:
1. Function Declaration
2. Function Definition
3. Function Calling
2. User Defined Functions Chapter 01
1. Function Declaration:
• Each function before usage must be declared (created) first, then we can define and
call the function.
• The declaration tells the compiler about a function name , return type, parameters
of the function.
• Syntax of function declaration:
return_type function_name ( parameter list );
Example:
int sum(int n);
2. User Defined Functions Chapter 01
Cont….
Return Type: is the data type of the value, that the function returns.
Some functions perform the desired operations without returning a value. In this case,
the return_type is the keyword void.
Function Name: This is the name of the function, which is given by the programmer.
Parameters: The values which are defined at the time of the function declaration is
called as parameters.
2. User Defined Functions Chapter 01
Examples:
void sum (int, int);
int largest (float, float, float);
Note: If a user-defined function, is declared after the main() function, an error will occur.
2. User Defined Functions Chapter 01
2. Function Definition:
• Function definition tells what task the function will be performing.
• The actual code of the function will be in function definition, it means we are
going to write the main code in order to achieve the purpose of the function.
• Remember that function definition must be always come after the function
declaration.
2. User Defined Functions Chapter 01
Cont…..
Syntax of function definition:
Return_type function_name ( parameter list )
{
// code statement
// code statement
}
2. User Defined Functions Chapter 01
Cont…..
Example:
void sum(int a, int b)
{
int c = a+b;
cout << “ Total is: “ << c << endl;
}
2. User Defined Functions Chapter 01
3. Function calling:
• A function gets life only when it is called.
• To call a function, write the function's name followed by two parentheses () and a
semicolon ;
Syntax:
<function-name> (argument list);
• Argument list is the values passed to the function.
2. User Defined Functions Chapter 01
3. Function calling:
• The number and data-types of arguments must be the same as the
parameters defined in function definition.
Example:
sum (12, 6);
2. User Defined Functions Chapter 01
Cont…
• If the function returns a value then the function calling must be written
using the following syntax in order to preserve the returned values.
Variable_name = function_name(argument list);
#include <iostream>
Example 1
using namespace std;
// Function declaration
void myFunction();
// The main method
int main() {
myFunction(); // call the function
return 0; }
// Function definition
void myFunction() {
cout << "I just got executed!";
}
#include <iostream>
Example 2
using namespace std;
// function declaration
void sum(int, int);
int main() {
sum(24, 25); // function call
}
// function definition
void sum(int a, int b) {
int c = a+b;
cout<<c;
}
2. User Defined Functions Chapter 01
Example 3
#include <iostream>
using namespace std;
float sum (float, float); // function declaration
int main() {
float a, b, total;
cout<<"Enter two numbers : \n";
cin>>a; cin>>b;
total = sum(a, b); // function call
cout<<"The sum of "<<a<<" and "<<b<<" is : "<<total;
}
float sum(float no1, float no2) // function definations
{
float c = no1 + no2;
}
Advantages of Function Chapter 01
Code Reusability: We can call a function many times. So we don't need
to write the same code again and again.
Code optimization: The length of program can be reduced using
functions.
Making program easier to understand and maintain.
Increases the readability of the program.
Function may be reused in multiple programs.
Debugging error is easy using function.
OOP Chapter 01
Thank You
For Your Patience