Function III

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/12

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 4:24 PM on 4/1/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

13 Terms

1
New cards

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

<p>the</p><ul><li><p>return type</p></li><li><p>function name (also a signature) </p></li><li><p>function signature (name, no. of parameters and it’s data type)</p></li></ul><p></p><p>the entire function header is called the function prototype</p><p></p>
2
New cards

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

3
New cards

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

4
New cards

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

5
New cards

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:

  1. function name

  2. function return type

  3. 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
}

6
New cards

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 types

7
New cards

can two functions have the same name but different different signature?

yes → function overloading

8
New cards

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 

9
New cards

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);

10
New cards

Function Resolution

compares actual parameters (passed in function call) vs. formal parameters

  1. First checks: exact match

  2. if no then checks: after promotion (char/bool/short → int) or (float → double)
    cannot be int → char/bool/short

  3. 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

11
New cards

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

12
New cards

void area(int length, int width = 5, int height = 1); which call correctly sets length=10 and height=2?

  1. area(10,2)

  2. area(10, ,2)

  3. area(10,5,2)

  4. 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

13
New cards

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

Explore top notes

note
The Pearl - Summary Notes
Updated 1161d ago
0.0(0)
note
Finance Class Notes
Updated 1377d ago
0.0(0)
note
SAT Test Taking Guide
Updated 386d ago
0.0(0)
note
Excretion
Updated 1336d ago
0.0(0)
note
science
Updated 1268d ago
0.0(0)
note
The Pearl - Summary Notes
Updated 1161d ago
0.0(0)
note
Finance Class Notes
Updated 1377d ago
0.0(0)
note
SAT Test Taking Guide
Updated 386d ago
0.0(0)
note
Excretion
Updated 1336d ago
0.0(0)
note
science
Updated 1268d ago
0.0(0)

Explore top flashcards

flashcards
Stories Information
102
Updated 1059d ago
0.0(0)
flashcards
BIO 106 Final
49
Updated 852d ago
0.0(0)
flashcards
APUSH Period 3
43
Updated 1198d ago
0.0(0)
flashcards
Biology Review
72
Updated 1167d ago
0.0(0)
flashcards
Georgia Test-Southern History
22
Updated 768d ago
0.0(0)
flashcards
Biologi: växtprov v.49
58
Updated 1213d ago
0.0(0)
flashcards
Stories Information
102
Updated 1059d ago
0.0(0)
flashcards
BIO 106 Final
49
Updated 852d ago
0.0(0)
flashcards
APUSH Period 3
43
Updated 1198d ago
0.0(0)
flashcards
Biology Review
72
Updated 1167d ago
0.0(0)
flashcards
Georgia Test-Southern History
22
Updated 768d ago
0.0(0)
flashcards
Biologi: växtprov v.49
58
Updated 1213d ago
0.0(0)