Download presentation
Presentation is loading. Please wait.
Published byFerdinand Norris Modified over 6 years ago
1
CSC113: Computer Programming (Theory = 03, Lab = 01)
Momina Moetesum Computer Science Department Bahria University, Islamabad
2
User Defined functions
Week # 9
3
User defined functions
Function definitions Function prototypes Function calls
4
Create customized functions to
Function Definitions Create customized functions to Take in data Perform operations Return the result Format for function definition: return-value-type function-name( parameter-list ) { declarations and statements } Example: int square( int y) { return y * y; }
5
Function Prototypes Function prototype Example:
Function name Parameters Information the function takes in Return type Type of information the function passes back to caller (default int) void signifies the function returns nothing Only needed if function definition comes after the function call in the program Example: int maximum( int, int, int ); Takes in 3 ints Returns an int
7
Example of user defined functions
int square ( int y ) { return y*y; }
8
Self practice Create a function to: Add two variables of type int
Add three variables of type float Multiply two variables of type double
9
examples int add ( int x, int y ) { return x+y; } ************************ float add (float x, float y, float z ) return x+y+z; double multiply ( double x, double y ) return x*y;
10
Test drivers Whenever you create your own function, you should immediately test it with a simple program. Such programs are called “test drivers”. Temporary or adhoc programs without comments or prompts etc. “Quick and dirty”
11
1. Function prototype 2. Loop Function definition Function call
3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 int square( int ); 9 10 int main() 11 { 12 for ( int x = 1; x <= 10; x++ ) cout << square( x ) << " "; 14 15 cout << endl; 16 return 0; 17 } 18 19 20 int square( int y ) 21 { 22 return y * y; 23 } 1. Function prototype 2. Loop Function definition Function call Program Output
12
Self practice Create test drivers for the programs created previously i.e.: int add (int , int); float add (float, float, float); double multiply (double, double);
13
Function categories Week # 9
14
Functions Library functions User defined functions Recursive functions
Inline functions Functions with empty parameter list
15
Recursion Recursive functions
functions that directly or indirectly call themselves Can only solve a base case If not base case, the function breaks the problem into a slightly smaller, slightly simpler, problem that resembles the original problem and Launches a new copy of itself to work on the smaller problem, slowly converging towards the base case Makes a call to itself inside the return statement Eventually the base case gets solved and then that value works its way back up to solve the whole problem
16
Examples of Recursion Example: factorial
n! = n * ( n – 1 ) * ( n – 2 ) * … * 1 Recursive relationship ( n! = n * ( n – 1 )! ) 5! = 5 * 4! 4! = 4 * 3!… Base case (1! = 0! = 1)
17
Factorial function - an example of recursion
//recursive factorial function #include<iostream> #include<conio.h> using namespace std; unsigned long factorial (unsigned long); void main( ) { for( int i=0; i<=10; i++) cout<<factorial(i)<<endl; getch( ); } unsigned long factorial (unsigned long number) if(number <= 1) return 1; else return number * factorial(number-1);
18
Examples of Recursion Fibonacci series: 0, 1, 1, 2, 3, 5, 8...
Each number sum of two previous ones Example of a recursive formula: fib(n) = fib(n-1) + fib(n-2)
19
Fibonacci function - an example of recursion
//recursive fibonacci series function #include<iostream> #include<conio.h> using namespace std; unsigned long fibonacci (unsigned long); void main( ) { unsigned long result, number; cout<<“Enter an integer:”; cin>>number; result=fibonacci (number); cout<<“Fibonacci(“ <<number<< “)”<<result<<endl; getch( ); } unsigned long fibonacci (unsigned long n) if(n== 0 || n==1) return n; else return fibonacci(n-1) + fibonacci(n-2);
20
Fibonacci function - an example of recursion
Diagram of Fibonnaci function f( 3 ) f( 1 ) f( 2 ) f( 0 ) return 1 return 0 return +
21
Program Output Enter an integer: 0 Fibonacci(0) = 0
Enter an integer: 6 Fibonacci(6) = 8 Enter an integer: 20 Fibonacci(20) = 6765 Enter an integer: 30 Fibonacci(30) = Enter an integer: 35 Fibonacci(35) =
22
Recursion vs. Iteration
Uses selection structure Terminates when base case is recognized Repeatedly calls itself Easier to understand and debug Computationally heavy in terms of memory and processing Uses repetition structure Terminates when loop condition fails Mostly uses counter controlled loop Not so apparent Computationally less heavy
23
Functions with Empty Parameter Lists
Either writing void or leaving a parameter list empty indicates that the function takes no arguments void print(); or void print( void ); Function print takes no arguments and returns no value
24
Notice the two ways of declaring no arguments.
1 // Fig. 3.18: fig03_18.cpp 2 // Functions that take no arguments 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 void function1(); 9 void function2( void ); 10 11 int main() 12 { 13 function1(); 14 function2(); 15 16 return 0; 17 } 18 19 void function1() 20 { 21 cout << "function1 takes no arguments" << endl; 22 } 23 24 void function2( void ) 25 { 26 cout << "function2 also takes no arguments" << endl; 27 } 1. Function prototypes (take no arguments) 2. Call the functions Function definitions Program Output Notice the two ways of declaring no arguments. function1 takes no arguments function2 also takes no arguments
25
Inline Functions inline functions Reduce function-call overhead
Asks the compiler to copy code into program instead of using a function call Compiler can ignore inline Should be used with small, often-used functions
26
Fibonacci function - an example of recursion
//inline function #include<iostream> #include<conio.h> using namespace std; inline float cube(const float s) {return s*s*s;} void main( ) { cout<<“Enter the side length of your cube:” ; float side; cin>>side; cout<<“Volume of cube with side”<<side<<“ is” <<cube(side)<<endl; getch( ); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.