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

Functions

The document explains the concept of functions in programming, highlighting their purpose in reducing code redundancy, enhancing code readability, and simplifying maintenance. It covers function declaration syntax, types of functions (library and user-defined), and the distinction between formal and actual parameters, along with parameter passing methods (call by value and call by reference). Examples in C++ illustrate the use of functions, including prototypes and the main function as the entry point of a program.

Uploaded by

Votex 2099
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Functions

The document explains the concept of functions in programming, highlighting their purpose in reducing code redundancy, enhancing code readability, and simplifying maintenance. It covers function declaration syntax, types of functions (library and user-defined), and the distinction between formal and actual parameters, along with parameter passing methods (call by value and call by reference). Examples in C++ illustrate the use of functions, including prototypes and the main function as the entry point of a program.

Uploaded by

Votex 2099
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Functions

A function is a block of code that performs a specific task. The idea is to put some
commonly or repeatedly done tasks together to make a function so that instead of
writing the same code again and again for different inputs, we can call this function.

Why are functions used?

• If some functionality is performed at multiple places in software, then


rather than writing the same code, again and again, we create a function
and call it everywhere. This helps reduce code redundancy.
• Functions make maintenance of code easy as we have to change at one
place if we make future changes to the functionality.
• Functions make the code more readable and easier to understand.

The syntax for function declaration is-

return-type
The return type of a function is the data type of the variable that that function returns.
For e.g.
if we write a function that adds 2 integers and returns their sum then the return type
of this function will be ‘int’ as we will returning sum that is an integer value.
When a function does not return any value, in that case the return type of the function
is ‘void’.

function_name
It is the unique name of that function.
It is always recommended to declare a function before it is used.
Parameters
A function can take some parameters as inputs. These parameters are specified along
with their data types.
For e.g.
if we are writing a function to add 2 integers, the parameters would be passed like –

int add (int num1, int num2)

Function Prototype
In C++, the code of function declaration should be before the function call. However,
if we want to define a function after the function call, we need to use the function
prototype.
The syntax of a function prototype is:

returnType functionName(dataType1, dataType2, ...);

Example:
#include<bits/stdc++.h>
using namespace std;
// using function definition after main() function
// function prototype is declared before main()

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

int main() {
int sum;

// calling the function and storing


// the returned value in sum
sum = add(100, 78);

cout << "100 + 78 = " << sum << endl;

return 0;
}
// function definition
int add(int a, int b) {
return (a + b);
}

Main function
The main function is a special function as the computer starts running the code from
the beginning of the main function. Main function serves as the entry point for the
program.

Types of Function: -

Library Functions:
Library functions are the built-in functions in C++ programming.
Programmers can use library functions by invoking the functions directly; they don't
need to write the functions themselves.
Some common library functions in C++ are sqrt(), abs(), isdigit(), etc.
In order to use library functions, we usually need to include the header file in which
these library functions are defined.
For instance, in order to use mathematical functions such as sqrt() and abs(), we need
to include the header file math.h.

User-Defined Functions:
These functions are created by the C programmer to perform specific tasks. By
defining custom functions, programmers can modularize their code, making it more
organized and easier to manage. User-defined functions contribute to code
optimization and reduce the complexity of large programs. They are designed to be
reusable, allowing the programmer to invoke them multiple times as needed.

Parameters :-

Formal Parameter
Formal Parameter refers to a variable and its type as they are declared in the
function or method prototype.

Actual Parameter
Actual Parameter, on the other hand, is the variable or expression associated
with a formal parameter during a function or method call in the calling
environment.

Types of function based on parameter passing


1. Call by value
2. Call by reference
1. Call by Value
In simpler terms, when we use call by value, it means that if you pass a variable to
a function, the function works with a copy of that variable. Any changes made to
the variable inside the function don't affect the original variable in the main part
of the program. It's like giving the function a snapshot of the variable to work with,
but the function can't change the original picture. This method is called "call by
value."
#include <bits/stdc++.h>
using namespace std;

void swap(int a, int b) {


int temp = a;
a = b;
b = temp;

cout<<"In swap function : a = "<<a<<" b = "<<b<<endl;;


}

int main() {
int a = 40;
int b = 50;
cout << "Before swap: a = " << a << " b = " << b << endl;
swap(a, b);
cout << "After swap: a = " << a << " b = " << b << endl;
return 0;
}

2. Call by Reference
In simpler terms, when we use call by reference, it means that if you pass a
variable to a function, the function gets to directly interact with the original
variable. Any changes made to the variable inside the function affect the original
variable in the main part of the program. It's like sharing the same piece of paper
between the function and the main program, so changes made by one are
immediately visible to the other. This method is called "call by reference," and it's
efficient in both time and space.
#include <bits/stdc++.h>
using namespace std;
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
cout<<"In swap function : a = "<<*a<<" b = "<<*b<<endl;;
}
int main() {
int x = 5;
int y = 10;
cout << "Before swap: x = " << x << " , y = " << y << endl;
swap(&x, &y);
cout << "After swap: x = " << x << ", y = " << y << endl;
return 0;
}

You might also like