Third
Third
Function Definition: A block of code that performs a specific task. Functions provide
code reusability.
Function Declaration: The prototype of the function that specifies the function's
name, return type, and parameters.
Passing Values: Data can be passed to a function through parameters (either by value
or by reference).
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Sum: " << add(num1, num2); // Function call
return 0;
}
Output:
Variables Execution 1 Execution 2 Execution 3
num1
num2
Output of function
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Maximum: " << findMax(num1, num2);
return 0;
}
Output:
Variables Execution 1 Execution 2 Execution 3
num1
num2
Output of function
Output:
Practice Problems
1. Write a function to calculate the factorial value of any integer entered through the
keyboard.
2. Write a function power ( a, b ), to calculate the value of a raised to b.
3. Write a function that receives marks received by a student in 3 subjects and returns
the average and percentage of these marks. Call this function from main ( ) and print
the results in main ( )