Functions in Cpp
Functions in programming are self-contained blocks of code that perform specific tasks. They provide modularity and code reusability by breaking complex programs into smaller, manageable modules. Functions encapsulate operations, abstracting away implementation details and enhancing code organization and readability. They promote efficient and optimized code by allowing for fine-tuning and optimization of specific tasks. Functions also enable collaboration and parallel development in team projects. Functions are essential in programming as they improve the code structure and maintainability and promote efficient and reusable code.
Function Declaration and Definition
A. Function declaration syntax
The function declaration provides the function's signature or prototype, specifying its name, return type, and parameters (if any).
Syntax:
return_type function_name(parameter_list);
The return type represents the data type of the value the function will return, such as int, void, float, etc.
The function name is an identifier that uniquely identifies the function.
The parameter list (if any) includes the data types, and names of the variables passed to the function.
B. Function definition syntax
The function definition contains the actual implementation of the function's code.
Syntax:
return_type function_name(parameter_list) {
// Function body (code)
}
C. Parameters and return types in function declaration and definition
Parameters : They are variables declared in the function's parameter list and are used to receive values from the calling code.
Return type : It specifies the value's data type that the function will return to the calling code. If no value is returned, the return type is void.
D. Examples illustrating function declaration and definition
Example 1: Function without parameters and return type:
int add() {
int n1, n2;
return n1 + n2;
}
Example 2: Function with parameters and return type:
int addNumbers(int num1, int num2) {
int sum = num1 + num2;
return sum;
}
Example 3: Function with parameters and no return type (void):
void printName (string name) {
cout << "Name: " << name << endl;
}
Function Calling and Arguments
A. Syntax for calling a function
To call a function, use the function name followed by parentheses.
Syntax: function_name(argument_list);
The argument list contains the values or variables passed to the function.
B. Arguments and their types
Arguments are the values or variables passed to a function during its invocation.
The type and number of arguments should match the function's parameter list.
Arguments can be literals (constants), variables, or expressions.
C. Passing arguments by value and by reference
Passing arguments by value:
By default, C++ functions pass arguments by value.
The function receives a copy of the argument, and any modifications made within the function do not affect the original value.
This is suitable for simple data types like integers, floats, characters, etc.
Passing arguments by reference:
Passing arguments by reference allows the function to modify the original value.
It is accomplished by using references or pointers as function parameters.
Changes made to the referenced value within the function affect the original value.
This is useful when passing large data structures or when modifications need to be reflected outside the function.
D. Examples of function calling with different arguments
Example 1: Function with arguments passed by value:
int addNumbers (int num1, int num2) {
int sum = num1 + num2;
return sum;
}
int main() {
int result = add Numbers (5, 7);
cout << "Sum: " << result << endl;
return 0;
}
Example 2: Function with arguments passed by reference:
void incrementByOne (int& num) {
num++;
}
int main() {
int value = 5;
incrementByOne(value);
cout << "Value after increment: " << value <<< endl;
return 0;
}
Example 3: Function with arguments passed by reference using pointers:
void swapValues(int *a, int* b) {
int temp = *a;
*a = *b;
*b= temp;
}
int main() {
int x = 5, y = 7;
swapValues(&x, &y);
cout << "Swapped values: " << x << ", " << y << endl;
return 0;
}