0% found this document useful (0 votes)
34 views

Submitted By: Name Muhammad Muheeb Javed Reg - No FA19-BCS-057 Section B Submitted To: MR - Usman Ali

Functions allow programmers to break programs into smaller, reusable parts (modules). This makes code more organized and easier to debug and maintain. The main() function is required in all C++ programs. User-defined functions perform specific tasks, like adding two numbers. Functions promote reusability so the same code can be used repeatedly without rewriting it.

Uploaded by

Muhammad Muheeb
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Submitted By: Name Muhammad Muheeb Javed Reg - No FA19-BCS-057 Section B Submitted To: MR - Usman Ali

Functions allow programmers to break programs into smaller, reusable parts (modules). This makes code more organized and easier to debug and maintain. The main() function is required in all C++ programs. User-defined functions perform specific tasks, like adding two numbers. Functions promote reusability so the same code can be used repeatedly without rewriting it.

Uploaded by

Muhammad Muheeb
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Assignment#3

Submitted by:
Name Muhammad Muheeb Javed
Reg.no FA19-BCS-057
Section B
Submitted to: Mr.Usman Ali

Q.No.1: Why we used function in C++?

A function is block of code that performs a specific task. C allows you to define
functions according to your need. These functions are known as user-defined
functions. For example: Suppose, you need to create a circle and color it depending
upon the radius and color.

Example: User-defined variables

1. #include <iostream>
2. using namespace std;
3.
4. // Function prototype (declaration)
5. int add(int, int);
6.
7. int main()
8. {
9. int num1, num2, sum;
10. cout<<"Enters two numbers to add: ";
11. cin >> num1 >> num2;
12.
13. // Function call
14. sum = add(num1, num2);
15. cout << "Sum = " << sum;
16. return 0;
17. }
18.
19. // Function definition
20. int add(int a, int b)
21. {
22. int add;
23. add = a + b;
24.
25. // Return statement
26. return add;
27. }

Q.No.2:What is the importance of function of C++?

A function is a group of statements that together perform a specific task.


Every C program has at least one function, which is main(). Why
use function ? Function are used for divide a large code into module, due to this we
can easily debug and maintain the code. Example highlights the two
most important reasons that C programmers use functions. The first reason is
reusability. Once a function is defined, it can be used over and over and over again.
You can invoke the same function many times in your program, which saves you
work.

You might also like