Functions in C++
What is Function?
A function is a group of statements that
together perform a task.
Every C++ program has at least one function,
which is main(), and all the most trivial
programs can define additional functions.
Defining a Function
return_type function_name( parameter list )
{
body of the function
}
Defining a Function
Return Type − A function may return a value.
The return_type is the data type of the value the function returns. Some
functions perform the desired operations without returning a value. In this
case, the return_type is the keyword void.
Function Name − This is the actual name of the function. The
function name and the parameter list together constitute the function
signature.
Parameters − A parameter is like a placeholder. When a function is
invoked, you pass a value to the parameter. This value is referred to as
actual parameter or argument. The parameter list refers to the type,
order, and number of the parameters of a function. Parameters are
optional; that is, a function may contain no parameters.
Function Body − The function body contains a collection of
statements that define what the function does.
Function Example
int max(int num1, int num2) {
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Function Declarations
A function declaration tells the compiler about a
function name and how to call the function. The
actual body of the function can be defined
separately.
return_type function_name( parameter list ) ;
Calling a Function
To call a function, you simply need to pass the
required parameters along with function name, and
if function returns a value, then you can store
returned value
int main () {
// local variable declaration:
int a = 100;
int b = 200;
int ret;
// calling a function to get max value.
ret = max(a, b);
cout << "Max value is : " << ret << endl;
return 0;
}
Function Arguments
Call Type Description
This method copies the actual value of an argument into the
formal parameter of the function. In this case, changes made
Call by Value to the parameter inside the function have no effect on the
argument.
This method copies the reference of an argument into the
Call by Reference formal parameter. Inside the function, the reference is used
to access the actual argument used in the call. This means
that changes made to the parameter affect the argument.
Function Overloading
C++ allows you to specify more than one definition
for a function name which is called function
overloading.
An overloaded declaration is a declaration that is
declared with the same name as a previously
declared declaration in the same scope, except that
both declarations have different arguments and
obviously different definition.
Function Overloading
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing double: " << f << endl;
}
void print(char* c) {
cout << "Printing character: " << c << endl;
}
Function Overloading
int main(void) {
// Call print to print integer
print(5);
// Call print to print float
print(500.263);
// Call print to print character
print("Hello C++");
return 0;
}
Function Questions
• In the following declaration, what is the return type?
int myMethod(int count, double value) { return 4;}
– A. Int
– B. MyMethod
– C. 4
– D. Double
– E. Count
Function Questions
In the following declaration, what is the return type?
int myMethod(int count, double value) { return 4;}
– A. Int
– B. MyMethod
– C. 4
– D. Double
– E. Count
Function Questions
Write a function declaration for the following
description (just the first line). The wording must be
exact and correct!
• The method must be called calculateFinalExam. The method
must return a double. The method accepts 3 parameters:
1. the studentId which must be an integer
2. the studentName which must be a String
3. the testScore which must be a double.
Function Questions
Write a function declaration for the following
description (just the first line). The wording must be
exact and correct!
• The method must be called calculateFinalExam. The method
must return a double. The method accepts 3 parameters:
1. the studentId which must be an integer
2. the studentName which must be a String
3. the testScore which must be a double.
• double calculateFinalExam (int studentId, string
studentName, double testScore);
Function Questions
What is the value that is returned in the
following function?
int getVolume() {
int v;
v = 23;
if (v < 50) {
return 50;
}
return v;
}
Function Questions
What is the value that is returned in the
following function?
int getVolume() {
int v;
v = 23;
if (v < 50) {
return 50;
}
return v;
}
50
Function Questions
How many can max number of arguments present in function
in the c++ compiler?
a) 99
b) 90
c) 102
d) 127
Function Questions
How many can max number of arguments present in function
in the c99 compiler?
a) 99
b) 90
c) 102
d) 127
Function Questions
Function Questions
10
Function Questions
Function Questions
Error
Function Questions
Function overloading can also be achieved if two or
more functions differ only in their return types.
a. True
b. False
Function Questions
Function overloading can also be achieved if two or
more functions differ only in their return types.
a. True
b. False
Function Exercises
Write a program that asks a name say hello.
Use your own function, that receives a string
of characters (name) and prints on screen the
hello message. (Doesn't returns anything- void
type)
Function Exercises
Write a program that ask for a number, and
show if the number is even or odd. Declare a
function called even_odd that return even or
odd of the number.
Function Exercises
Write a program that calculates power of
number Ex- 5^4 =625 . Declare your own
function to do this.
Function Exercises
Write a program that ask for two numbers,
and assigned these two numbers to x and y
.Declare swap function to swap values of x
and y.
Assignment
Write a program that use overload function
Area, to compute area of circle, rectangle and
square.
Assignment
Write a program to Compute Combinations
using Factorials. prints a total number of
combination possible for given n and k value.