0% found this document useful (0 votes)
11 views4 pages

Functions in Programming

Uploaded by

RK
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)
11 views4 pages

Functions in Programming

Uploaded by

RK
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/ 4

Functions in Programming

Functions are fundamental building blocks in programming, providing a way to


encapsulate code into reusable and manageable units. They help in improving
readability, maintainability, and reducing redundancy.

Definition

A function is a block of code that performs a specific task, can be invoked or called
from other parts of the program, and may return a value.

Components of a Function

1.​ Function Declaration (Prototype): Specifies the function’s name, return type,
and parameters (if any) without providing the actual implementation.
2.​ Function Definition: Contains the actual implementation of the function,
including the code to be executed when the function is called.
3.​ Function Call: The process of invoking a function to execute its code.

Function Declaration

A function declaration specifies the name, return type, and parameters of the
function, informing the compiler about the function’s interface.

// Function declaration
return_type function_name(parameter_list);

Example:
int add(int a, int b);

Function Definition

The function definition includes the actual code that gets executed when the
function is called.

// Function definition
return_type function_name(parameter_list) {
​ // Function body
​ // Code to be executed
}

Example:
int add(int a, int b) {
​ return a + b;
}

Function Call
A function call executes the code defined within a function.

// Function call
function_name(arguments);

Example:
int result = add(5, 3);

Types of Functions

1.​ Built-in Functions: Provided by the programming language or its standard


library (e.g., printf in C, cout in C++).
2.​ User-defined Functions: Created by the programmer to perform specific
tasks.

Function Parameters

1.​ Formal Parameters: Variables defined in the function declaration/definition.


2.​ Actual Parameters (Arguments): Values passed to the function when it is
called.

Parameter Passing

1.​ Pass-by-Value: Copies the actual parameter’s value to the formal parameter.
Changes made to the formal parameter do not affect the actual parameter.

void func(int x) {
​ x = 10;
}

int main() {
​ int a = 5;
​ func(a);
​ cout << a; // Output: 5 (a remains unchanged)
}

2.​ Pass-by-Reference: Passes the actual parameter itself to the function,


allowing changes made to the formal parameter to affect the actual
parameter.

void func(int &x) {


​ x = 10;
}

int main() {
​ int a = 5;
​ func(a);
​ cout << a; // Output: 10 (a is changed)
}

3.​ Pass-by-Pointer: Passes the memory address of the actual parameter to the
function, allowing the function to modify the original value.

void func(int *x) {


​ *x = 10;
}

int main() {
​ int a = 5;
​ func(&a);
​ cout << a; // Output: 10 (a is changed)
}

Return Type

The return type of a function specifies the type of value the function will return. If a
function does not return any value, its return type is void.

Example:
void display() {
​ cout << “Hello, World!”;
}
int add(int a, int b) {
​ return a + b;
}

Recursion

A function that calls itself is said to be recursive. Recursion is useful for problems
that can be divided into smaller subproblems of the same type.
Example:
int factorial(int n) {
​ if (n <= 1)
​ return 1;
​ else
​ return n * factorial(n – 1);
}

Inline Functions
Inline functions are functions defined with the inline keyword, suggesting the
compiler to insert the function’s body where the function is called, to reduce the
overhead of function calls.
Example:
inline int square(int x) {
​ return x * x;
}

Function Overloading

Function overloading allows multiple functions with the same name but different
parameter lists. The correct function to call is determined by the number and type of
arguments.
Example:
int add(int a, int b) {
​ return a + b;
}
double add(double a, double b) {
​ return a + b;
}
int main() {
​ cout << add(5, 3); // Calls the first add function
​ cout << add(5.0, 3.0); // Calls the second add function
​ return 0;
}

You might also like