KEMBAR78
Programming Fundamentals lecture-10.pptx
Programming Fundamentals
LECTURE-10
1 GHULAM ABBAS
Abbas.sbcws@gmail.com
2
In the Previous Lecture
Nested loop
Magic with while loop
Conditional Operator
Break and continue statement
goto statement
3
Today’s lecture
Functions
Importance of functions
Type of functions
Function prototype/declaration
Function Definition
Passing arguments to functions
Returning values from the functions
Calling the functions
Call by value
Call by reference
4
Laboratory Stool
5
Constructing a laboratory Stool
6 Constructing a laboratory Stool
 Task: Making a stool
 Subtask:
 Make a seat
 Make legs for the stool
 Assemble them
7
Functions
 Complicated and large program is divide into smaller unit/modules by giving
unique name to them.
 The unite can be invoked from other part of program.
 Most importance to use function is to aid conceptual organization of
program.
 Another use to functions is to reduce program size.
 Single function can be called again and again from multiple places.
 The function’s code is stored at only one place of memory.
 Program is written without using functions may be repetition of the same
piece of code at various places.
8 Type of functions
Functions are divided into two types in C++
Built-in functions
User-defined functions
9 Built-in functions
 The predefined functions are the part of programing language.
 Also referred as library functions
 C++ has a large number of built-in functions.
 These functions are declared in header file.
 The function declaration must be included in header file if a specific
function is to be used.
 Example sqrt()-> to compute square root, must include math.h
 Similary exit()-> to exit from the program, must include process.h
10 User-defined functions
 A programmer can write his/her own functions to perform a
specific task.
 Also referred programmer-defined functions
 User-defined functions contain:
Function declaration or prototype
Function definition
11 Function declaration or prototype
 Like variable, functions should be declared before defined.
return_type function_name( parameter list);
main()
{
……
}
12
Function definition
 The definition is actual code,.
 Written for the function to perform specific task.
 Can written following places
Before main() function,
After main() function
Separate file ( should be included using #include
13
Syntax of function definition:
return_type function_name( parameter
list)
{
declarations and statements
}
14
E
x
a
m
p
l
e
#include<iostream>
using namespace std;
void starLine();
main()
{
starLine();
cout<<"S.NOt Student Name"<<endl;
starLine();
cout<<"1t Mr. Ahmad Ali"<<endl;
cout<<"2t Mr. Muhammad Hussain"<<endl;
cout<<"3t Mr. Najaf Ali"<<endl;
starLine();
}
void starLine()
{
for(int j=0;j<35;j++)
cout<<"*";
cout<<endl;
}
Function
declaration
Function
Definition
Output
Function call
Function call
15
Function
types of functions:
1. Functions that return a value and take no
parameter
2. Functions that return a value and take parameter
3. Functions that do not return a value and take no
parameter
4. Functions that do not return a value and take
parameter
16
Functions that return a value and take no parameter (Example)
int myFunction ( ) ;
void main ( )
{
int val, n=5;
val= myFunction();
cout<<val+n;
}
int myFunction ( )
{
int a=12, b=14;
return a+b;
}
17
Functions that return a value and take parameter (Example)
int myFunction (int, float ) ;
void main ( )
{
int val, n=5;
val= myFunction(12, 14);
cout<<val+n;
}
int myFunction ( int a, float b)
{
return a+b;
}
18
Functions that do not return a value and take no parameter (Example)
Void myFunction() ;
void main ( )
{
myFunction();
}
myFunction ()
{
Int a=14, b=12;
cout<< a+b;
}
19
Functions that do not return a value and take parameter (Example)
myFunction (int, float ) ;
void main ( )
{
int n1=5, n2=6;
myFunction(n1,
n2);
}
myFunction ( int a, float b)
{
cout<< a+b;
}
20
int val ,n1 , n2;
val = myFunction();
val= myFunction(12, 14);
myFunction(n1, n2);
myFunction();
Function Call
21
double raiseToPow ( double x , int power )
{
double result ;
int i ;
result = 1.0 ;
for ( i = 1 ; i <= power ; i ++ ) // braces first
{
result * = x ; // result = result *x
}
return ( result ) ;
}
Example: Function to calculate integer power ( Xn
)
22
include < iostream.h >
void main ( )
{
double x ;
int i ;
cout << “ Please enter the number “ ;
cin >> x ;
cout << “ Please enter the integer power that you want this number raised to “ ;
cin >> i ;
cout << x << “ raise to power “ << i << “is equal to “ << raiseToPow ( x , i ) ;
}
Code to Call the raisetopower Function
23
#include <iostream.h >
int f ( int ) ;
main ( )
{
int i = 10 ;
cout << “In main i = " << i ;
f ( i ) ;
cout << " Back in main, i = " << i ;
}
Example: Call by Value
24
int f ( int i )
{
cout << "In function f , i = " << i ;
i *= 2 ;
cout << "In function f , i is now = “ <<
i ;
return i ;
}
Example: Call by Value
25
double square ( double x )
{
return x * x ;
}
main ( )
{
double number = 123.456 ;
cout << “ The square of “ << number << “ is “<< square
( number ) ;
cout << “ The current value of “ << number << “is “ << number
;
}
Example : Square of a Number
26
A function in which original value of the
variable is changed
To call by reference we cannot pass value,
we have to pass memory address of
variable
“&” is used to take the address of a variable
Call by Reference
27
main ( )
{
double x = 123.456 ;
square ( &x ) ;
}
Value of ‘x’ is not passed , but the memory address of
‘x’ is passed
Example: Call by Reference
28
square ( double *x )
{
*x = *x * *x ;
}
Example: Call by Reference
x is a pointer to a variable double
29
 Pointers are used to pass address of variable for
reference
 We use “ &x ” to send the address of “ x “
 To receive the address we use “ *x ”
(whatever “ x ” points to)
Pointers

Programming Fundamentals lecture-10.pptx

  • 1.
  • 2.
    2 In the PreviousLecture Nested loop Magic with while loop Conditional Operator Break and continue statement goto statement
  • 3.
    3 Today’s lecture Functions Importance offunctions Type of functions Function prototype/declaration Function Definition Passing arguments to functions Returning values from the functions Calling the functions Call by value Call by reference
  • 4.
  • 5.
  • 6.
    6 Constructing alaboratory Stool  Task: Making a stool  Subtask:  Make a seat  Make legs for the stool  Assemble them
  • 7.
    7 Functions  Complicated andlarge program is divide into smaller unit/modules by giving unique name to them.  The unite can be invoked from other part of program.  Most importance to use function is to aid conceptual organization of program.  Another use to functions is to reduce program size.  Single function can be called again and again from multiple places.  The function’s code is stored at only one place of memory.  Program is written without using functions may be repetition of the same piece of code at various places.
  • 8.
    8 Type offunctions Functions are divided into two types in C++ Built-in functions User-defined functions
  • 9.
    9 Built-in functions The predefined functions are the part of programing language.  Also referred as library functions  C++ has a large number of built-in functions.  These functions are declared in header file.  The function declaration must be included in header file if a specific function is to be used.  Example sqrt()-> to compute square root, must include math.h  Similary exit()-> to exit from the program, must include process.h
  • 10.
    10 User-defined functions A programmer can write his/her own functions to perform a specific task.  Also referred programmer-defined functions  User-defined functions contain: Function declaration or prototype Function definition
  • 11.
    11 Function declarationor prototype  Like variable, functions should be declared before defined. return_type function_name( parameter list); main() { …… }
  • 12.
    12 Function definition  Thedefinition is actual code,.  Written for the function to perform specific task.  Can written following places Before main() function, After main() function Separate file ( should be included using #include
  • 13.
    13 Syntax of functiondefinition: return_type function_name( parameter list) { declarations and statements }
  • 14.
    14 E x a m p l e #include<iostream> using namespace std; voidstarLine(); main() { starLine(); cout<<"S.NOt Student Name"<<endl; starLine(); cout<<"1t Mr. Ahmad Ali"<<endl; cout<<"2t Mr. Muhammad Hussain"<<endl; cout<<"3t Mr. Najaf Ali"<<endl; starLine(); } void starLine() { for(int j=0;j<35;j++) cout<<"*"; cout<<endl; } Function declaration Function Definition Output Function call Function call
  • 15.
    15 Function types of functions: 1.Functions that return a value and take no parameter 2. Functions that return a value and take parameter 3. Functions that do not return a value and take no parameter 4. Functions that do not return a value and take parameter
  • 16.
    16 Functions that returna value and take no parameter (Example) int myFunction ( ) ; void main ( ) { int val, n=5; val= myFunction(); cout<<val+n; } int myFunction ( ) { int a=12, b=14; return a+b; }
  • 17.
    17 Functions that returna value and take parameter (Example) int myFunction (int, float ) ; void main ( ) { int val, n=5; val= myFunction(12, 14); cout<<val+n; } int myFunction ( int a, float b) { return a+b; }
  • 18.
    18 Functions that donot return a value and take no parameter (Example) Void myFunction() ; void main ( ) { myFunction(); } myFunction () { Int a=14, b=12; cout<< a+b; }
  • 19.
    19 Functions that donot return a value and take parameter (Example) myFunction (int, float ) ; void main ( ) { int n1=5, n2=6; myFunction(n1, n2); } myFunction ( int a, float b) { cout<< a+b; }
  • 20.
    20 int val ,n1, n2; val = myFunction(); val= myFunction(12, 14); myFunction(n1, n2); myFunction(); Function Call
  • 21.
    21 double raiseToPow (double x , int power ) { double result ; int i ; result = 1.0 ; for ( i = 1 ; i <= power ; i ++ ) // braces first { result * = x ; // result = result *x } return ( result ) ; } Example: Function to calculate integer power ( Xn )
  • 22.
    22 include < iostream.h> void main ( ) { double x ; int i ; cout << “ Please enter the number “ ; cin >> x ; cout << “ Please enter the integer power that you want this number raised to “ ; cin >> i ; cout << x << “ raise to power “ << i << “is equal to “ << raiseToPow ( x , i ) ; } Code to Call the raisetopower Function
  • 23.
    23 #include <iostream.h > intf ( int ) ; main ( ) { int i = 10 ; cout << “In main i = " << i ; f ( i ) ; cout << " Back in main, i = " << i ; } Example: Call by Value
  • 24.
    24 int f (int i ) { cout << "In function f , i = " << i ; i *= 2 ; cout << "In function f , i is now = “ << i ; return i ; } Example: Call by Value
  • 25.
    25 double square (double x ) { return x * x ; } main ( ) { double number = 123.456 ; cout << “ The square of “ << number << “ is “<< square ( number ) ; cout << “ The current value of “ << number << “is “ << number ; } Example : Square of a Number
  • 26.
    26 A function inwhich original value of the variable is changed To call by reference we cannot pass value, we have to pass memory address of variable “&” is used to take the address of a variable Call by Reference
  • 27.
    27 main ( ) { doublex = 123.456 ; square ( &x ) ; } Value of ‘x’ is not passed , but the memory address of ‘x’ is passed Example: Call by Reference
  • 28.
    28 square ( double*x ) { *x = *x * *x ; } Example: Call by Reference x is a pointer to a variable double
  • 29.
    29  Pointers areused to pass address of variable for reference  We use “ &x ” to send the address of “ x “  To receive the address we use “ *x ” (whatever “ x ” points to) Pointers