1/12
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
what is in a function?
the
return type
function name (also a signature)
function signature (name, no. of parameters and it’s data type)
the entire function header is called the function prototype

Function Declaration
when we only have the function header/function prototype
WITHOUT any body statements
void func1(int x);it should end with a semicolon, we’re just letting compiler know this function exists
Function Definition
When we have a function header AND body statements that will be executed
int sum(int a, int b){
return a+b;
}every function definition is also a function declaration
since we have a function header, and we have body statements
but not every function declaration is a definition
Function Definition VS Function Declaration
we can declare a function as many times as we want
but can only define it once
define = header + body statements
declare = header only
all functions must be declared before they can be used
before we call a function, the compiler needs to know it exists by looking at:
function name
function return type
parameters
thus, if funcA calls funcB inside, but funcB isn’t defined yet, we can simply declare funcB before funcA
int funcB(int b);
int funcA(int a){
funcB();
}
int funcB(int b){
return b;
}Or, we can declare funcB inside funcA…
int funcA(int a){
int funcB(int b); //declaring funcB inside
funcB(); //thus allowing us to call funcB
}can two functions have same signature (name, no. of parameters, type of parameters) but different return types?
no
int pick_one(int x, float y) { return x; }
float pick_one(int x, float y) { return y; }
//error, cannot have same signature but diff return typescan two functions have the same name but different different signature?
yes → function overloading
Function Overloading
multiple functions can have the same name, but different type of parameter
int max(int a, int b);
int max(int a, int b, int c);
double max(double a, double b);
void swap(int& a, int& b);
void swap(float& a, float& b);
void swap(double& a, double&);parameter name doesn’t matter, type and order of the type matter
can’t have
int max(int a, int b);
int max(int c, int d);NOTE:
also get an error if a function has the same parameters but is a pass by reference version
int max(int a);
int max(int& a);
//error, doesn't know which one to pass max() to is this allowed
int sum(double a, double b);
double sum(double& a, double&b);this is NOT allowed, error → invalid function overloading
return type does not matter, error because it has same datatype order (double and double& reference are basically the same)
compiler error, not because of what we’re passing
but it the compiler doesn’t account for the &, so it just sees two functions with two doubles as parameter
but this would work:
void process(double& x);
void process(const double& x);Function Resolution
compares actual parameters (passed in function call) vs. formal parameters
First checks: exact match
if no then checks: after promotion (char/bool/short → int) or (float → double)
cannot be int → char/bool/short
still no then checks: standard conversion (int → float/double → int) or (double → float)
void func(float x);
void func(double x);
int main() {
func(5); // ERROR: Ambiguous!
}error, since both func does standard conversion int → float/double, both same rank, it doesn’t know which one to pick, thus error
Default Arguments
must be at the end of the formal parameter list
must only have default arguments in either
function declaration
function definition
but not in both
int max(int a = 5);
int max(int a = 10){
return a;
}
//if both have default args == error
//instead do:
int max(int a = 5);
int max(int a){
return a;
}it is best practice to only give default parameters to function declaration, if can
void area(int length, int width = 5, int height = 1); which call correctly sets length=10 and height=2?
area(10,2)
area(10, ,2)
area(10,5,2)
area(10, width = 5, 2)
correct answer area(10,5,2)
if we want to manually set the most right, the left must all be manually defined
What is the outcome of the following?
void set(int& x);
void set(const int& x);
int main() {
int n = 5;
set(n);
}
answer: void set(int& x);
when passing a non-const, it is always matches the non-constant