Functions
Functions are a fundamental feature in the procedural programming, acting as the primary building blocks used to organize code into logical and reusable code. This allows developers break complex code into smaller and simpler logics.
Simple functions
return_type function_name(parameter_list){
# code ...
}
return_typecan any, void, primitive types, or user-defined types, pointers, etc.function_namemust be any valid identifier besidemain, see main.parameter_listis optional, can contain none or more parameters.
Example: In this example, we're going to create a sum function to perform addition on two integers.
int sum(int a, int b){
return a + b;
}
int main(string args[]){
int result = add(1, 2); # result = 3
return 0;
}
void, otherwise it will result in compilation error.Default value parameters
return_type function_name(type parameter_name = value){
}
- A default value must be a constant, which is known at compile time.
- A parameter with default value will become optional, can be skipped when calling.
- All parameters with default values must be defined at the end of the parameter list.
Example: In this example below, we're going to create a scale function to muliply a number by a factor.
float scale(float a, float factor = 1.0){
return a * factor;
}
int main(){
float r1 = scale(3.0); # r1 = 3.0 * 1.0 = 3.0
float r2 = scale(3.0, 1.5); # r2 = 3.0 * 1.5 = 4.5
return 0;
}
default keyword
default is a keyword used to instruct the compiler to substitute a parameter's default value at the call site.
Example: In this code below, we have a function print_text with 3 parameters, two of which has their own default values.
void print_text(string text, float opacity = 1.0, int size = 12){
# implementation ...
}
opacity and size have a default value, meaning they can be skipped when calling print_text. However, it's not possible to skip opacity if size need a new value rather than its default one. In a case that you want to keep opacity as default but manually looking for its default value can be a troublesome. This is where the default keyword comes to save the day.
int main(string args[]){
print_text("hello, world!\n");
print_text("hello, world!", default, 15);
return 0;
}
default keyword removes the need to look for the actual default value of a parameter, as the compiler will substitute it during compilation.default can not be passed into an argument that does not have a default value.Function Overloading
Overloaded functions are funtions that share exact same name but different parameters. And each function has its own unique signature, which a combination of the function's name and parameters.
Example
int add(int a, int b){
return a + b;
}
int add(int a, int b, int c){
return a + b + c;
}
In this example, we define two functions sharing the same name, but each has different signature because of the different parameters. And you can define many functions sharing the same name as many as you want as long as they have different parameters.
Now look at another example
int add(int a, int b){
return a + b;
}
float add(int a, int b){
return a + b;
}
However, this will cause compilation error becuase two functions sharing the same signature exist at the same scope. Although they have different return types, it does not change the function signature as long as the parameters stay the same.
- changes in type.
- changes in number of parameters.
Caution
void func(int a, float b){
# code ...
}
void func(int x, float y){
# code ...
}
void func(float m, int n){
# code ...
}
func above are the same, and this will result in compilation error!