[go: up one dir, main page]

0% found this document useful (0 votes)
20 views11 pages

The Functions in C++ (Presentation Slides)

The document is a presentation on functions in C++ programming, detailing their structure, types, and benefits. It explains the importance of functions for code reusability, readability, modularity, and debugging, and distinguishes between user-defined and system-defined functions. Additionally, it clarifies the difference between parameters and arguments, illustrating these concepts with examples and code comparisons.

Uploaded by

zhavilia5
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)
20 views11 pages

The Functions in C++ (Presentation Slides)

The document is a presentation on functions in C++ programming, detailing their structure, types, and benefits. It explains the importance of functions for code reusability, readability, modularity, and debugging, and distinguishes between user-defined and system-defined functions. Additionally, it clarifies the difference between parameters and arguments, illustrating these concepts with examples and code comparisons.

Uploaded by

zhavilia5
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
You are on page 1/ 11

FUNDAMENTAL PROGRAMMING (Course Code: IT-104)

Title: Functions
Presented by Madam Warda

1st Semester Members:


Group #1 • Sarosh Zahoor (2320)
• Khaista (2306)
• M. Mujahid Iqbal (2333)
• M. Hussain Anwar (2313)
Functions in Programming
(C++)
Table of Contents:

1. What is a Function?
2. Function Structure Components
3. Types of Functions
4. Function Types in Action
5. Why Use Functions?
6. Code Comparison: With vs Without Functions
7. Parameters vs Arguments
8. More on Parameters vs Arguments
1. What is a Function?
A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a
function. Functions are used to perform certain actions, and they are important for reusing code: Define the code
once, and use it many times.

Syntax Purpose Benefits


void myFunction() to make programs modular, Functions in C++ make code
{// code to be executed} reusable, and easier to reusable, organized, easier to
understand by dividing tasks into read, and simplify debugging by
smaller blocks of code. dividing tasks into smaller parts.

Parts of a C++ Function Declaration Definition


A C++ function consists of two A function declaration tells the Contains the body of the function,
main parts: compiler about a function's name, which is the code to be executed.
return type, and parameters.
2. Function Structure Components

#include <iostream>
Function Calling
using namespace std;
means telling the program to run the
code of that function. When you call a
function, the control of the program
// Function Prototype
jumps to the function, executes its
void greet(); code, and then comes back to continue
the rest of the program.

int main() {
// Function Calling Function Body
Contains the actual code that executes
greet();
when the function is called, enclosed in
return 0; curly braces with the implementation
logic.
}

// Function Body (Definition) Function Prototype


void greet() { Declares the function and tell the
compiler about the function’s name,
cout << "Hello, welcome to C++!" << endl; return type, and parameters before the
}
main function. Example: int add(int a,
int b);
3. Types of Functions

User-Defined Functions System-Defined Functions


Functions created by programmers to perform specific Pre-built functions provided by the C++ standard library.
tasks according to their requirements. These are custom- These are ready-to-use functions for common
built solutions for particular problems. programming tasks.
• Created by the programmer • Built into the language
• Tailored to specific needs • Optimised and tested
• Can be modified as required • Available through headers
4. Function Types in Action
User-Defined Example System-Defined Example

// Custom function to calculate area int main() {


double calculateArea(double length, double double number = 16.0;
width) { double root = sqrt(number); // sqrt() is
return length * width; system-defined
} cout << "Square root: " << root;
return 0;
int main() { }
double area = calculateArea(10.5, 8.2); //
Function call with arguments
cout << "Area: " << area;
return 0;
}

User-defined functions solve specific problems, whilst system-defined functions like sqrt(), pow(), and abs() handle common
mathematical operations.
5. Why Use Functions?

Reusability Readability

Write once, use multiple times. Functions eliminate the need to Functions make code easier to understand by breaking
rewrite the same code repeatedly, saving time and effort in complex logic into smaller, named blocks with clear purposes.
development.

Modularity Debugging

Programs become modular, allowing developers to work on Isolating code in functions makes it easier to identify, locate,
different parts independently and combine them seamlessly. and fix bugs without affecting other parts of the program.
6. Code Comparison: With vs Without Functions
Without Functions (Repetitive) With Functions (Clean)

int main() { // Function to calculate area


// Calculate area 1 int calculateArea(int length, int width) {
int length1 = 10, width1 = 5; return length * width;
int area1 = length1 * width1; }
cout << "Area 1: " << area1 << endl;
int main() {
// Calculate area 2 cout << "Area 1: " << calculateArea(10, 5) <<
int length2 = 8, width2 = 6; endl;
int area2 = length2 * width2; cout << "Area 2: " << calculateArea(8, 6) << endl;
cout << "Area 2: " << area2 << endl; cout << "Area 3: " << calculateArea(12, 4) <<
endl;
// Calculate area 3 return 0;
int length3 = 12, width3 = 4; }
int area3 = length3 * width3;
cout << "Area 3: " << area3 << endl;

return 0;
}

The function-based approach reduces code from 15 lines to 8 lines whilst improving maintainability and readability significantly.
7. Parameters vs Arguments

Parameters Arguments
Parameters are specified after the function name, inside the Actual values passed to the function when it's called during program
parentheses. You can add as many parameters as you want, just execution.If a function uses arguments, it must declare variables to
separate them with a comma:Data can be given to functions through receive them, called formal parameters. These act like local
parameters. These parameters work like variables that hold values variables, created when the function starts and destroyed when it
inside the function. ends.

int add(int a, int b) { int result = add(5, 3);


// ↑ ↑ // ↑ ↑
// parameters // arguments
return a + b;}

Key Difference: Parameters are the formal variables in function definitions, whilst arguments are the actual values passed during function calls.
8. More on Parameters and Arguments

In C++, parameters and arguments are closely related but slightly different. Parameters are the variables
defined inside the function header, which act as placeholders to receive values. They exist only while the
function runs and behave like local variables. For example, in int add(int x, int y), the variables x and y
are parameters.

On the other hand, arguments are the actual values or variables passed to the function when it is called.
For example, in add(5, 3), the numbers 5 and 3 are arguments. The arguments are copied into the
parameters, and then the function uses them to perform its task.

This distinction makes functions flexible: parameters allow the function to be defined once, while
arguments let you provide different inputs each time you call it. This way, a single function can handle
many different values without rewriting the code.
Thank You!
Any Questions?

You might also like