Download programming functions and more Schemes and Mind Maps C programming in PDF only on Docsity!
So far we have seen
- What is a C++ Function?
- <math.h> Library Functions
- Function Prototype - 1
- Function Definition - 2
- Function Call -
- Function Call by Value
- Function Call by Reference
- Taking input from user : Example
- Prime Number Function : Example
- Output Function : Example
- Function Over Loading
- Function Over Loading : sum Examples
- Function with Default Argument
- Scope of Variable Department of Computer Sciences Bahria University, Islamabad
CSC 113 – Computer Programming
- More on Functions Slide: 2
Contents for today
Function Recap
Scope of Variable
Arrays as Function Parameters
Pointers as Function Parameters
Recursion
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 3 Inline functions Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 4
end main
#include <iostream.h>
inline double cube( double side )
return side * side * side; // calculate cube
int main()
cout << "Enter the side length of your cube: "; double sideValue;
cin >> sideValue;
// calculate cube of sideValue and display result cout << "Volume of cube with side "
<< sideValue << " is " << cube( sideValue ) << endl;
return 0; } //
Volume of cube with side 3.5 is 42.
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 7
Enter the side length of your cube: 3.
Overloaded Functions Function overloading Functions with same name and different parameters Should perform similar tasks
E.g. function to square ints and function to square floats
int square( int x) {return x * x;} float square(float
x) { return x * x; }
Overloaded functions distinguished by signature Based on name and parameter types (order matters) Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 8
Overloaded Functions
Examples:
int sum(int , int);
int sum (int, int,
int); float sum (int,
float);
double multiply (float ,
float);float multiply (float ,
float);
Wrong !!
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 9
// Using overloaded
functions. #include
<iostream.h>
// function square for int values int square( int x )
cout << "Called square with int argument: " << x << endl;
return x * x;
} // end int version of function square
// function square for double values double square( double y )
cout << "Called square with double argument: " << y << endl;
return y * y;
} // end double version of function square
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 10
mputer Sciences CSC^113 –^ Compu Islamabad 11.
Contd…
If the function prototype is omitted because the function definition also serves as the prototype, then the
default arguments should be specified in the function header.
Default arguments also can be used with inline functions.
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 14
// Using default arguments.
#include
// function prototype that specifies default arguments
int boxVolume( int length = 1, int width = 1, int height = 1 ); 8
9void main()
// no arguments--use default values for all dimensions
cout << "The default box volume is: " << boxVolume(); 13
// specify length; default width and height
cout << "\n\nThe volume of a box with length 10,\n"
<< "width 1 and height 1 is: " << boxVolume( 10 ); 17
// specify length and width; default height
cout << "\n\nThe volume of a box with length 10,\n"
<< "width 5 and height 1 is: " << boxVolume( 10, 5 );
Department of Co Bahria University, ter Programming More on Functions Slide: 15
mputer Sciences CSC^113 –^ Compu Islamabad 11.
Department of Co Bahria University, ter Programming ore on Functions Slide: 16
M
The default box volume is: 1 The volume of a box with length 10, width 1 and height 1 is: 10 The volume of a box with length 10, width 5 and height 1 is: 50 The volume of a box with length 10, width 5 and height 2 is: 100
} // end main
// function boxVolume calculates the volume of a box
int boxVolume( int length, int width, int height )
return length * width * height;
} // end function boxVolume
// specify all arguments
cout << "\n\nThe volume of a box with length 10,\n"
<< "width 5 and height 2 is: " << boxVolume( 10, 5, 2 )
<< endl;
Variables and Storage Classes
Variables have attributes
Have seen name, type, size, value
Storage class\Lifetime
How long variable exists in memory
Scope
Where variable can be referenced in program
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 18
Variables and Storage Classes
Static Variables
Local Variables in functions
Keeps its value between function calls
Only known in own function
Life – Entire program execution
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 21 Scope Rules Scope
Portion of program where identifier can be used
File scope
Defined outside a function, known in all functions
Global variables, function definitions and prototypes
Function scope
Can only be referenced inside defining function
Block scope
Begins at declaration, ends at right brace }
Can only be referenced in this range
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 22
mputer Sciences CSC^113 –^ Compu Islamabad 11. mputer Sciences CSC^113 –^ Compu Islamabad 11.
Department of Co Bahria University,
25 cout << "local x in main's outer scope is " << x << endl; 26 27 useLocal(); // useLocal has local x 28 useStaticLocal(); // useStaticLocal has static local x 29 useGlobal(); // useGlobal uses global x 30 useLocal(); // useLocal reinitializes its local x 31 useStaticLocal(); // static local x retains its prior value 32 useGlobal(); // global x also retains its value 33 34 cout << "\nlocal x in main is " << x << endl; 35 36 37 } // end main 38 39 // useLocal reinitializes local variable x during each call 40 void useLocal( void ) 41 { 42 int x = 25 ; // initialized each time useLocal is called 43 44 cout << endl << "local x is " << x 45 << " on entering useLocal" << endl; 46 ++x; 47 cout << "local x is " << x 48 << " on exiting useLocal" << endl; 49 50 } // end function useLocal 51 ter Programming More on Functions Slide: 24
// A scoping example. #include <iostream.h> 4 void useLocal( void );// function prototype void useStaticLocal( void ); // function prototype void useGlobal( void );// function prototype 8 9 int x = 1;// global variable 10 11 void main() 12 { 13 14 15 16 17 18 19 20 21 22 23 int x = 5;// local variable to main cout << "local x in main's outer scope is " << x << endl; { // start new scope int x = 7; cout << "local x in main's inner scope is " << x << endl; } // end new scope Department of Co Bahria University, ter Programming More on Functions Slide: 23
// Global Variables // ^^^^^^^^^^^^^^^^^^^^^1^^^^^^^^^^^^^^^^^^^^^^ ^^ void checkStaticVariable(); void anotherFunction(); // int main() { checkStaticVariable( ); checkStaticVariable( ); anotherFunction(); checkStaticVariable( ); checkStaticVariable( ); anotherFunction(); _getch(); } //^^^^^^^^^^^^^^^^^^^^^2^^^^^^^^^^^^^^^^^^^^^^^^ // void checkStaticVariable() { static int myStaticVariable = 100; cout << "$$Value of My Static Variable is : " << myStaticVariable << endl; myStaticVariable += 50; cout << "$$Value of My Static Variable is : " << myStaticVariable << endl; } void anotherFunction() { static int myStaticVariable = 1100; cout << "Value of My Static Variable is : " << myStaticVariable << endl; myStaticVariable += 150; cout << "Value of My Static Variable is : " << myStaticVariable << endl; } Department of Computer Sciences Bahria University, Islamabad
Functions
Recap
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 28
CSC 113 – Computer Programming
- More on Functions Slide: 27
C++ Functions
C++ allows the use of both internal (user-defined) and external functions.
External functions (e.g., abs , ceil , rand , sqrt , etc.) are usually grouped into specialized libraries (e.g.,
iostream, math , etc.)
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 31 User defined functions
C++ programs usually have the following form:
// include statements
// function prototypes
// main() function
// function
definitions
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 32
Function Input & Output Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 33 Function Definition
A function definition has the following syntax:
(
For example: Definition of a function that computes the absolute value of an integer:
int absolute(int x)
if (x >= 0)return x; else return -
x;
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 34
Function Prototype
The function prototype declares the input and output parameters of the function.
The function prototype has the following syntax:
();
Example: A function that returns the absolute value of an integer is: int absolute(int);
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 37 Function Prototype
The function definition can be placed anywhere in the program after the function prototypes.
If a function definition is placed before main(), there is no need to include its function prototype.
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 38
Return Value
A function normally returns a single result, if it is not a void function.
At least one of the statements in the function body should have the form:
return ;
The value passed back by the return statement should have the same type as the function.
Department of Computer Sciences Bahria University, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 39 Functions
Call By Value
Copy of data passed to function Changes to copy do not change original Prevent unwanted side effects Department of Computer Sciences Ba F h u r n ia ct U io n n iv s ersity, Islamabad CSC 113 – Computer Programming
- More on Functions Slide: 40