CPP CH#5
CPP CH#5
CPP CH#5
Computer Programming
(ECEg 1013)
Course Instructor:
Haylemaryam G. (M.Sc. in ECEg)
Fiche, Ethiopia
May 27, 2024
CHAPTER V
Functions
➢ Function Declaration and Definition
➢ Passing Value by Value and Passing Value by Reference
➢ Function Overloading
➢ Default Argument
➢ Inline Function and Recursive Functions
Example:
x = AddTwoNumbers();
➢ NB. We can define a function bellow the main function but the
declaration must be before the main function, i.e. declaration is
prototype.
Pass-by-Value
➢ Means passing copies of value of variables but never the variables
themselves.
➢ The variable is unaffected by the function. Thus the variable is a read only
parameter.
int r;
r = a+b;
return (r);
}
int addition (int a, int b, int c)
{
int r;
r = a+b+c;
return (r);
}
int main ()
{ Output
int z; float y;
z = addition (5,3);
y = addition (5.5,3);
cout<<"The result is "<<z;
return 0;
}
By: Haylemaryam G. (MSc in ECEg.) 32
Default Argument
➢ Default argument is a programming convenience which removes the
burden of having to specify argument values for all function parameters.
➢ This is the function without argument.
➢ The default value of the argument is specified in the function prototype
/declaration.
➢ The default arguments are passed to the function if the argument is
missing on the function call.
➢ The arguments are default from right to left.
➢ This tells the compiler to replace each call to the function with explicit code
for the function. Use it only for very short functions.
By: Haylemaryam G. (MSc in ECEg.) 36
Example 1:
//actually be compiled as though it were //this
#include<iostream> program:
using namespace std;
inline int cube(int x) #include<iostream>
{
return x*x*x; using namespace std;
} int main()
int main() {
{ cout<<(4)*(4)*(4)<<endl;
cout<<cube(4)<<endl; int x, y;
int x,y;
cin>>x;
cin>>x;
y = cube(2*x-3); y = (2*x+3)*(2*x+3)*(2*x+3);
} return 0;
}
1) 2)