0% found this document useful (0 votes)
41 views

Function With Default Arguments: Example

1) A function can have default arguments to allow it to be called without specifying all arguments. Default values are provided in the function declaration. 2) A recursive function calls itself to perform a computation, with a termination step to end recursion and an inductive step that calls the function with a reduced parameter value. 3) Function overloading allows using the same function name for different tasks by defining functions with the same name but different argument lists. This allows a function to perform different operations based on the data types passed to it.

Uploaded by

saif jamal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Function With Default Arguments: Example

1) A function can have default arguments to allow it to be called without specifying all arguments. Default values are provided in the function declaration. 2) A recursive function calls itself to perform a computation, with a termination step to end recursion and an inductive step that calls the function with a reduced parameter value. 3) Function overloading allows using the same function name for different tasks by defining functions with the same name but different argument lists. This allows a function to perform different operations based on the data types passed to it.

Uploaded by

saif jamal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Structured Programming Lecture 7

Function with default arguments


A function can be called without specifying all its arguments. This can be
achieved only if the function declaration provides default values for those
arguments that are not specified.

Example:
#include <iostream.h>
void repchar(char='*', int=45); //declaration with
//default arguments
void main()
{
repchar(); //prints 45 asterisks
repchar('='); //prints 45 equal signs
repchar('+', 30); //prints 30 plus signs
}

void repchar(char ch, int n)


{
for(int j=0; j<n; j++)
cout << ch;
cout << endl;
}

Note:
• If one argument is missing when the function is called, it is
assumed to be the last argument.
• The missing arguments must be the trailing arguments—those at
the end of the argument list.

Recursive Function
A recursive function is a function that calls itself in order to perform a
task of computation. There are two basic components of a recursive
solution:

Asst. Lec. Wasseem Nahy Ibrahem Page 1


Structured Programming Lecture 7

• Termination step, stating the solution when the process comes to an


end.
• Inductive step, calling the function itself with a renewed (lesser)
parameter value.

Example: Write a C++ program that computes the factorial of a positive


integer number using the recursive function factorial().
#include <iostream.h>
long factorial(int);
void main()
{
int number;
cout<<"Enter a positive integer number: ";
cin>>number;
cout<<"The factorial is " << factorial(number);
}

long factorial (int n) Termination


{ Step
if (n == 0)
return 1; Inductive
else Step
return (n * factorial(n - 1));
}

Calling the function factorial with argument 4, i.e. factorial(4):

Asst. Lec. Wasseem Nahy Ibrahem Page 2


Structured Programming Lecture 7

Important Notes
• Recursive functions must have a termination step and an inductive
step.
• Recursive function calls in inductive step must signify a reduction
of argument value.
• Be aware of infinite recursion problem.
• Recursion is implicit while iteration is explicit.
• Recursion is slower than iteration.

Example: Write a C++ program that counts the number of digits of an


entered integer number using the recursive function countingDigits().
#include<iostream.h>
int countingDigits(long);
void main()
{
long number;
cout<<"Enter an integer number: ";
cin>>number;
cout<<"No. of digits is " <<countingDigits(number)
<<endl;
}

int countingDigits(long n)
{
if (n / 10 == 0)
return 1;
else
return( countingDigits(n/10) + 1 );
}

Asst. Lec. Wasseem Nahy Ibrahem Page 3


Structured Programming Lecture 7

Function Overloading
Overloading refers to the use of the same thing for different purposes.
Function overloading means that we can use the same function name to
create functions that perform a variety of different tasks. These
overloaded functions have the same function name but with different
argument lists (i.e. different number and/or different data types of
arguments). An overloaded function appears to perform different
activities depending on the kind of data sent (passed) to it. It performs
one operation on one kind of data but another operation on a different
kind.

Example: Write a C++ program that computes the area of square and the
area of rectangle using the overloaded function area().
#include<iostream.h>
int area(int);
int area(int , int);
void main()
{
int length , width;
cout<<"Enter a length of square: ";
cin>>length;
cout<<"The area of square is " <<area(length)<<endl;
cout<<"Enter a length and width of rectangle: ";
cin>>length>>width;
cout<<"The area of rectangle is "
<<area(length,width)<<endl;
}

int area(int a)
{
return (a * a);
}

int area(int a , int b)


{
return (a * b);
}

Asst. Lec. Wasseem Nahy Ibrahem Page 4


Structured Programming Lecture 7

Example: Write a C++ program that computes the volume of cube,


cylinder, and rectangle using the overloaded function volume().
#include<iostream.h>
int volume(int);
double volume(double , int);
long volume(long, int, int);
void main()
{
cout<< volume(10) <<endl
<< volume(2.5 , 8) <<endl
<< volume(100, 75, 15) << endl;
}

int volume(int s)
{
return (s * s * s);
}

double volume(double r , int h)


{
return (3.14519 * r * r * h);
}

long volume(long l, int b, int h)


{
return (l * b * h);
}

Homework:
1. Write a C++ program that computes the power of an entered integer
number using the recursive function power().
2. Write a C++ program that adds two numbers of different numeric data
types (e.g. integers, float, double) using the overloaded function
add().

Asst. Lec. Wasseem Nahy Ibrahem Page 5

You might also like