Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This lecture was delivered by Prof. Usman Younis at Quaid-i-Azam University. This lecture covers following points of course Object Oriented Programming using C plus plus: Functions, Sub-Routines, Statements, Organization, Declaration, Prototype, Call, Function, Definition, Body
Typology: Slides
1 / 11
Functions or sub- routines are a set of instructions / statements which can be grouped into units It helps reducing the program size, and improves program organization
//function //call func(); . . . //function //call func(); .
func() {
..... }
Usman Younis
Function Components
Component Purpose Example
Declaration (prototype)
Specifies the function name, its arguments, and return value types. Declaration alerts the compiler that the function is coming later
int square(int a);
Call Causes the function to be executed
int y = square(2);
Function definition
Body of function int square(int a) { return a*a; }
Function Arguments
Function can be called by passing constant values in arguments E.g., void function (char a, int b); //declaration And the function call can be function(+
, 24); Similarly the function call can be invoked by passing values as arguments char c = +
; int x = 24; function(c, x); In both cases, the function will create new variables, and the values passed by the function call will be assigned to them
Usman Younis
Reference Arguments
Arguments can be passed by Reference in a function call E.g., void add(int, int, int&); int a, b, sum; add(a, b, sum); cout<<sum; void add(int a1, int b1, int& sum1) { sum1 = a1 + b1; }
In reference passing the memory address of the variable is passed to the function call It is similar as passing a pointer to the variable address
Reference Vs. Pointers
A reference parameter is a constant pointer , i.e., after initializing this parameter, it can’t be changed int a = 20; int& Ref; Ref = a;
Ref += 20;
cout<<Ref; cout<<a;
40 40
Usman Younis
Reference Vs. Pointers (contd..)
int b = 30; Ref = b;
Ref += 20; b -= 30;
cout<<Ref; cout<<a; cout<<b;
50 50 0
Reference Vs. Pointers (contd..)
A pointer is variable which stores the address of a memory location It can be assigned address of any variable of the same data type for which the pointer is defined
int a = 0; int* ptr = &a;
*(ptr) += 30; a += 30;
cout<<a; cout<<*(ptr);
60 60
Usman Younis
Reference Vs. Pointers (contd..)
int b = 0; int* ptr = &b;
b = 20; *(ptr) += 30;
cout<<b; cout<<*(ptr); cout<<a;
50 50 60
Function Return
Computed values or data can be returned from a function using the return statement
float power(float number, int power) { float result = 1; for(int i = 0; i < power; i++) result *= number; return number; }
Similarly you can return different data types and structures using the return statement
Usman Younis
Overloaded Functions
Functions can be overloaded to perform different operations, depending upon the function call Overloading can be done using different number of arguments, or even different types of arguments
E.g., void function(); void function(int a); void function(int a, int b); void function(float a, float b);
Overloaded Functions (contd..)
void function() { cout<<“this function has no argument”; }
void function(int a) { cout<<“this function has an integer argument :”<<a; }
void function(int a, int b) { cout<<“this function has two integer arguments :”<<a<<“ and ”<<b; }
Usman Younis
Inline Functions
Used to save the execution time, e.g., in time critical applications you may require to avoid frequent jumps to the function body Writing inline in the function declaration/definition tells the compiler to add the code into the main() function of the program at each call E.g., inline void function(int, float);
Storage types
So far the data types (variables) have been declared/defined as external or automatic (internal) External: At the top (under #include statements), and visible to all the code which follow Automatic/Internal: In a function body, and visible to the code in the function body only What happen if you try to access an automatic variable outside the function body? ERROR!
Usman Younis
Storage types (contd..)
Static variables Visible inside a function body only, same as automatic variable However, lifetime is for the whole program, i.e., they retain their values even after the function call has ended (unlike automatic variables). E.g., static int a = 12;
Storage types (contd..)
Automatic / Internal
External Static
Visibility Function body Whole program
Function body
Lifetime Function body Whole program
Whole program Initial Value
Not initialized, arbitrary value
0 0
Usman Younis
Arrays
Arrays are used to group the data of similar types, e.g., an array of int, or an array of structures E.g., int arr[10]; //an array of 10 integers
struct shape { int length; int width; int height; }; shape box[200]; //an array of 200 boxes
Arrays (contd..)
Arrays are processed by accessing individual elements E.g., cin>>arr[5];//setting the value of 5 th^ element Or cout<<(box[20].lengthbox[20].widthbox[20].height); //displaying the volume of 20 th^ box
Arrays can be initialized at declaration/definition int arr[5] = {12, 32, 78, 1, 22};
Usman Younis
Arrays (contd..)
Arrays can be multi- dimensional E.g., shape Box[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
Multi-dimensional arrays are stored sequentially in the memory
Box[0][0]
1 2 3 4 5 6 7 8 9
Box[0][1] Box[0][2] Box[1][0] Box[1][1] Box[1][2] Box[2][0] Box[2][1] Box[2][2]
Box[3][3]
C-Strings
A string is an array of characters E.g., char str[100]; //An array of 100 characters cin>>str; //Input using cin cout<<“The input is : ”<<str; //output
String arrays must end with a null character , i.e., \0
The operator >> considers “space” to be a terminating character at the console input
C-Strings (contd..)
You can use cin.get() function to read a complete line containing blanks as well E.g., cin.get(str, maximum-characters); //the function will copy up to maximum //characters //from the console to string “str”
To read multiple lines cin.get(str, maximum-characters, ‘terminating- character’); //this will copy up to maximum-characters from console //to string “str”, until the terminating character has been //typed