محاضرة 10
Functions II
Advanced functions, recursion, and default parameters.
يلا نشوف الملخص
Lecture 10 Summary: Advanced Function Concepts
1. Advanced Parameters
- Multiple Parameters: Functions can take multiple inputs separated by commas. Parameters act as local variables inside the function.
- Default Parameter Values: You can assign a default value to a parameter using the equals sign (
=).- Usage: If an argument is omitted during the function call, the default value is used.
- Example:
void myFunc(string country = "Norway").
2. Passing Arrays to Functions
- When an array is passed to a function, C++ passes the address of the first element (pointer) rather than a copy of the whole array.
- Syntax:
void myFunction(int myNumbers[5]).
3. Recursion
- Definition: A programming technique where a function calls itself.
- Purpose: It provides a way to break complicated problems down into simpler, solvable problems.
- Mechanism: Each recursive call should move closer to a Base Case (a condition that stops the recursion).
- Example: A
sum(k)function that returnsk + sum(k-1)untilkis 0.
- Example: A
في نقطة مش واضحة؟ بطبط موجود!
اسأل بطبط عنها