08.Functions
08.Functions
Functions
Source: The C++ Workshop, A New, Interactive Approach to Learning C++,
https://www.tutorialspoint.com/cplusplus/cpp_functions.htm
https://www.w3schools.com/cpp/cpp_functions.asp
C A M A R I N E S S U R P O LY T E C H N I C C O L L E G E S | C O L L E G E O F C O M P U T E R S T U D I E S
What is a Function?
C++ Function
• group of statements that together perform a task. Example: main()
• The C++ standard library provides numerous built-in functions that your
program can call. For example, function strcat() to concatenate two strings,
function memcpy() to copy one memory location to another location and
many more functions.
// function declaration
void greet()
{
cout << "Hello World";
}
A C++ function definition consists of a function header and a function body. Here
are all the parts of 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.
A C++ function definition consists of a function header and a function body. Here
are all the parts of a function −
•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.
For the above defined function max(), following is the function declaration −
Parameter names are not important in function declaration only their type
is required, so following is also a valid declaration −
Function declaration is required when you define a function in one source file and you call that
function in another file. In such case, you should declare the function at the top of the file
calling the function.a
CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER STUDIES
Function
Create a Function
• C++ provides some pre-defined functions, such as main(). But you can also
create your own functions to perform certain actions.
void myFunction() {
// code to be executed
}
Calling a Function
• In the following example, myFunction() is used to print a text (the
action), when it is called:
// Create a function
void myFunction() {
cout << "I just got executed!";
}
int main() {
myFunction(); // call the function
return 0;
}
// Liam Refsnes
// Jenny Refsnes
// Anja Refsnes
int myFunction(int x) {
return 5 + x;
}
The void keyword, used in the previous
examples, indicates that the function should
int main() { not return a value. If you want the function to
cout << myFunction(3); return a value, you can use a data type (such
return 0; as int, string, etc.) instead of void, and use
} the return keyword inside the function:
// Outputs 8 (5 + 3)
// Outputs 8 (5 + 3)
Now, we can use either the variable name food or the reference name meal to refer to
the food variable:
return 0;
}
CCIT 102 – CP 1 CAMARINES SUR POLYTECHNIC COLLEGES | COLLEGE OF COMPUTER STUDIES
C++ Pointers
Some C++ tasks are performed more easily with pointers, and other C++ tasks,
such as dynamic memory allocation, cannot be performed without them.
every variable is a memory location and every memory location has its address defined
which can be accessed using ampersand (&) operator which denotes an address in
memory.
Consider the following which will print the address of the variables defined −
Here, type is the pointer's base type; it must be a valid C++ type and var-name is the
name of the pointer variable. Following are the valid pointer declaration −