2 - Functions
2 - Functions
What is 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) {
int result;
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 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.
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.
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