0% found this document useful (0 votes)
2 views6 pages

18 - Function Declaration

This lecture covers function declaration and calling in C++, including the syntax, prototype, parameters, and the importance of function declarations for compiler recognition. It explains the difference between function declaration and definition, as well as the roles of formal and actual parameters. Additionally, it highlights common mistakes and provides practice problems to reinforce learning.

Uploaded by

mazharp308
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

18 - Function Declaration

This lecture covers function declaration and calling in C++, including the syntax, prototype, parameters, and the importance of function declarations for compiler recognition. It explains the difference between function declaration and definition, as well as the roles of formal and actual parameters. Additionally, it highlights common mistakes and provides practice problems to reinforce learning.

Uploaded by

mazharp308
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Function Declaration & Calling

(Syntax, Prototype, Parameters)

1. Objective of This Lecture


In this lecture, students will learn:

 What is function declaration (prototype)


 Why function declaration is needed
 Syntax of function declaration
 How to call a function
 Role of parameters in functions

2. Recap: Function Definition


A function definition contains:

 Return type
 Function name
 Parameters
 Function body

Example:

int add(int a, int b)


{
return a + b;
}

3. What is Function Declaration (Prototype)?


A function declaration (also called prototype) tells the compiler:
✔ Function name
✔ Return type
✔ Number and type of parameters

➡ It does not contain the function body.


4. Why Do We Need Function Declaration?
In C++:

 main() executes first


 Compiler must know about a function before it is called

✔ Function declaration solves this problem

5. Function Without Declaration (Problem)


int main()
{
sum(); // Error: sum not known
return 0;
}

void sum()
{
cout << "Hello";
}

❌ Compiler error occurs

6. Function Declaration Solves the Problem


void sum(); // Function declaration

int main()
{
sum(); // Valid call
return 0;
}

void sum()
{
cout << "Hello";
}

✔ Compiler knows about the function


7. Syntax of Function Declaration
return_type function_name(parameter_list);

Example:

int add(int, int);

8. Important Rules for Function Prototype


✔ End with semicolon
✔ Parameter names are optional
✔ Order and data types must match definition

9. Examples of Function Declarations


void display();
int square(int);
float average(int, int);

10. What are Parameters?


Parameters are:

Values passed to a function for processing

✔ They act as input to the function

11. Types of Parameters (Basic Level)


✔ Formal parameters → in function definition
✔ Actual parameters → in function call

12. Example: Parameters


int add(int a, int b) // a and b are formal parameters
{
return a + b;
}

Function call:

add(5, 3); // 5 and 3 are actual parameters

13. Function Calling


Calling a function means:

Executing the code inside the function

Syntax:

function_name(arguments);

14. Example: Function Call


int result = add(10, 20);
cout << result;

15. Complete Program Example


#include <iostream>
using namespace std;

int add(int, int); // function declaration

int main()
{
int sum = add(4, 6);
cout << "Sum = " << sum;
return 0;
}

int add(int a, int b) // function definition


{
return a + b;
}

16. Execution Flow


1. Compiler reads function prototype
2. main() starts execution
3. Function call occurs
4. Control transfers to function
5. Function returns value
6. Control comes back to main()

17. Common Mistakes


❌ Missing semicolon in prototype
❌ Mismatch in parameter types
❌ Wrong return type
❌ Calling function with incorrect number of arguments

18. Important Exam Points


✔ Function declaration definition
✔ Syntax of prototype
✔ Difference between declaration and definition
✔ Formal vs actual parameters

19. Practice Problems


1. Declare and call a function to print a message
2. Write a function to find square of a number
3. Write a function with two parameters
4. Identify errors in given function declarations

20. Summary
 Function declaration informs compiler
 Prototype contains return type and parameters
 Parameters allow data passing
 Function calling executes the function

You might also like