What is Function?
A function is a self-contained block of code designed to perform a specific task. Functions allow for code modularization, which promotes better organization and avoids redundancy. The main function of any C or C++ program is called main()
, from which execution starts, but users can define and use their own functions as needed.
Syntax of a Function
A function in C or C++ has the following basic structure:
return_type function_name(parameter_list) {
// Body of the function
// Code to execute the task
return value; // Optional, depending on return type
}
- Return Type: Specifies the type of value the function will return (
int
,void
,float
, etc.). - Function Name: Name of the function, which is used to call the function.
- Parameter List: A list of inputs that the function can accept. Parameters are optional.
- Function Body: The block of code that performs the task.
- Return Statement: Specifies what the function returns, if applicable. Functions with
void
return type do not need to return anything.
Example of a Simple Function:
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 10);
std::cout << "Sum: " << result << std::endl;
return 0;
}
Types of Functions
In C and C++, functions can be classified based on their return types, parameter lists, or whether they are built-in or user-defined.
1 User-Defined Functions
These are functions created by the programmer to perform specific tasks.
int multiply(int x, int y) {
return x * y;
}
2 Library (Built-in) Functions
C and C++ come with a rich set of library functions that are provided by the standard library. For example, printf()
in C and std::cout
in C++.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Passing Arguments
There are three common ways to pass arguments to a function:
1 Pass by Value:
A copy of the argument is passed to the function, so changes to the parameter inside the function do not affect the original variable.
#include <iostream>
void increment(int x) {
x = x + 1;
}
int main() {
int num = 5;
increment(num);
std::cout << num << std::endl; // Output: 5
}
Output:
5
2 Pass by Reference (C++):
The function gets access to the original variable by passing a reference to it, allowing the function to modify the original data.
#include <iostream>
void increment(int& x) {
x = x + 1;
}
int main() {
int num = 5;
increment(num);
std::cout << num << std::endl; // Output: 6
}
Output:
6
3 Pass by Pointer:
A pointer to the variable is passed, allowing the function to modify the original variable via the pointer.
#include <iostream>
void increment(int* x) {
*x = *x + 1;
}
int main() {
int num = 5;
increment(&num);
std::cout << num << std::endl; // Output: 6
}
Output:
6
Return Types
1 Void Functions:
A function with void
return type does not return a value.
void printMessage() {
std::cout << "Hello, World!" << std::endl;
}
2 Returning Values:
Functions can return values of any data type, such as int
, float
, char
, or even user-defined types like structs
and classes
.
float divide(int a, int b) {
return (float) a / b;
}
3 Returning Multiple Values:
Although C and C++ do not support returning multiple values directly, this can be achieved using pointers, references, or user-defined structures (in C++).
Function Linkage
In C and C++, functions can have external linkage or internal linkage.
External Linkage:
Functions with external linkage can be called from other translation units (source files). By default, all global functions have external linkage.
void globalFunction() { // External linkage by default
// ...
}
Internal Linkage:
Using the static
keyword limits the function's scope to the current file, meaning it cannot be called from other files.
static void internalFunction() { // Internal linkage
// ...
}