Functions
COS2614 - Programming: Contemporary Concepts · Introduction to C++ and Qt 4
Functions
Functions are a fundamental part of programming in C++. They allow you to group code into reusable blocks. This makes your programs easier to read and maintain. Understanding functions is essential for writing efficient C++ code, especially when using the Qt framework.
Key idea: After studying this topic, you should be able to:
- Define what a function is in C++.
- Write function declarations and definitions.
- Understand function parameters and return types.
- Use function overloading.
- Differentiate between passing parameters by value and by reference.
What is a Function?
A function is a block of code that performs a specific task. It can take inputs, known as parameters, and can return an output. Functions help to organize code into manageable sections.
Function Declaration
A function must be declared before it is used. A function declaration tells the compiler about the function's name, return type, and parameters. Here is the syntax:
returnType functionName(parameterType1 parameterName1, parameterType2 parameterName2) {
// function body
}For example:
int add(int a, int b) {
return a + b;
}This function, add, takes two integer parameters and returns their sum.
Function Definition
The function definition includes the function’s body, which contains the actual code that will be executed. The declaration and definition can be separate, but they must match in terms of parameters and return type.
Calling a Function
To use a function, you call it by its name and provide the required arguments. For example:
int result = add(5, 3); // result will be 8Function Parameters
Parameters are the values you pass into a function. You can have zero or more parameters. When you call a function, you provide arguments that correspond to these parameters. There are two main ways to pass parameters:
1. Pass by Value
When you pass by value, a copy of the argument is made. Changes to the parameter inside the function do not affect the original argument.
void modifyValue(int x) {
x = x + 10;
}
int main() {
int a = 5;
modifyValue(a); // a remains 5
}2. Pass by Reference
When you pass by reference, you pass the address of the argument. Changes to the parameter affect the original argument.
void modifyValue(int &x) {
x = x + 10;
}
int main() {
int a = 5;
modifyValue(a); // a is now 15
}Watch out: Be careful when using pass by reference, as it can lead to unintended side effects if you modify the argument inside the function.
Function Return Types
The return type of a function indicates what type of value it will return. If a function does not return a value, its return type is void.
void displayMessage() {
cout << "Hello, World!";
}Function Overloading
C++ allows you to have multiple functions with the same name, as long as their parameter lists differ. This is known as function overloading.
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}In this example, both functions are named add, but they take different parameter types.
Inline Functions
An inline function is a function that is expanded in line when it is called. This can improve performance by avoiding the overhead of a function call.
inline int square(int x) {
return x * x;
}Example: Using Functions
Here is a complete example that demonstrates the concepts discussed:
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(10, 20);
cout << "The sum is: " << result << endl;
return 0;
}Destructor Example
Destructors are special member functions that are called when an object is destroyed. Here is an example using a class with a destructor:
class Thing {
public:
Thing(int n) : m_Num(n) { }
~Thing() {
cout << "Destructor called: " << m_Num << endl;
}
private:
int m_Num;
};
int main() {
Thing t1(1);
Thing t2(2);
return 0;
}In this example, when the objects t1 and t2 go out of scope, their destructors are called, and you will see output indicating that the destructors were called.
Watch out: Remember that destructors are automatically called when an object goes out of scope. If you allocate memory dynamically, ensure you manage it properly to avoid memory leaks.
Summary
- Functions are reusable blocks of code.
- Functions must be declared before use.
- Parameters can be passed by value or by reference.
- Functions can return values.
- Function overloading allows multiple functions with the same name.
Check your understanding
- What is the difference between passing parameters by value and by reference?
- What is function overloading?
- How do you declare a function in C++?
- What happens when an object goes out of scope in C++?