






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







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 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
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 (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