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

5 Functions

The document discusses functions in C/C++. It defines what a function is, the different types of functions, function declaration, definition and call. It also explains function parameters, return types, call by value, call by reference, default parameters, inline functions and passing structures to functions.

Uploaded by

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

5 Functions

The document discusses functions in C/C++. It defines what a function is, the different types of functions, function declaration, definition and call. It also explains function parameters, return types, call by value, call by reference, default parameters, inline functions and passing structures to functions.

Uploaded by

viveknegi200345
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Functions

Functions
• A function is a group of statements that together perform a task.
• By using functions, we can avoid rewriting same logic/code again and again in a
program.
• A function can be called multiple times to provide reusability.
• A function can improve the readability of code.
• Debugging of the code would be easier if you use functions, as errors are easy to
be traced.
• Reduces the size of the code, duplicate set of statements are replaced by function
calls.
Types of functions
• Library function:
These are the functions which are declared in the C header files. They are also known as build
in or predefined functions. Such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
• User-defined functions:
These are the functions which are created by the user at the time of writing the code, so that
he/she can reduces the complexity of a big program and optimizes the code.
• External functions are usually grouped into specialized libraries (e.g., iostream, stdlib,
math,etc.)
Structure of User define function

• • Function Declaration
• • Function Definition
• • Function Call
• Declaration/Syntax of function:
return_type function_name(parameter list)
{
//block of code
}
• return_type: Return type can be of any data type such as int, double, char, void, short etc.
• function_name: It can be anything, however it is advised to have a meaningful name for
the functions so that it would be easy to understand the purpose of function just by seeing
it’s name.
• parameter list: The parameter list declares the type and number of arguments that the
function expects when it is called. Also, the parameters in the parameter list receives the
argument values when the function is called. They are often referred as formal
parameters.
• Block of code: Set of C statements, which will be executed whenever a call will be made
to the function.
Function call
• Function can be called from anywhere in the program. The parameter list must not
differ in function calling and function declaration. We must pass the same number
of parameters as it is declared in the function declaration.
• When a program calls a function, the program control is transferred to the called
function. A called function performs a defined task and when its return statement is
executed or when its function-ending closing brace is reached, it returns the
program control back to the main program.
• Calling a function:
function_name (argument_list);
Arguments/Parameters in functions
• Arguments are the values specified during the function call
• It is possible to have a function with parameters but no return type. It is not
necessary, that if a function accepts parameter(s), it must return a result too.
Function demonstration:- Swapping of 2 numbers
• #include <iostream>
• using namespace std;
• void swap(int a, int b) { //here a and b are formal parameters
• b = a + b;
• a = b - a;
• b = b - a;
• cout<<"\nAfter swapping: ";
• cout<<"a = "<<a;
• cout<<"\tb = "<<b;
• return;
• }
• int main()
• {
• int a,b;
• cout<<"Enter the two numbers to be swapped: "; cin>>a>>b;
• cout<<"a = "<<a;
• cout<<"\tb = "<<b;
• swap(a,b); //here a and b are actual parameters
• }
Output:

• Enter the two numbers to be swapped: 5 3


•a=5b=3
• After swapping: a = 3 b = 5
Example of adding two numbers to demonstrate the return types.
• #include <iostream>
• using namespace std;
• int sum(int a, int b){
• return (a+b);
• }
• int main()
• {
• int a, b, result;
• cout<<"Enter the two numbers to be added: "; cin>>a>>b;
• result = sum(a,b);
• cout<<"\nSum of the two numbers : "<<result;
• }
• Output:
• Enter the two numbers to be added: 11 11
• Sum of the two numbers: 22
Example contd:

• In the above example, we have a function sum that takes two integer
parameters and returns an integer type.
• In the main function, we read two integers from the console input and pass
it to the sum function.
• As the return type is an integer, we have a result variable on the LHS and
RHS is a function call.
Call types of functions

• There are following ways of calling a function:


1) Call by value
2) Call by reference
Call by value
• In call by value method, the value of the actual parameters is copied into the formal
parameters. In other words, we can say that the value of the variable is used in the
function call in the call by value method.
• In call by value method, we can not modify the value of the actual parameter by the
formal parameter.
• In call by value, different memory is allocated for actual and formal parameters since
the value of the actual parameter is copied into the formal parameter.
• The actual parameter is the argument which is used in the function call whereas
formal parameter is the argument which is used in the function definition.
Example: #include<iostream>
using namespace std;
void calc(int x)
{
// changing the value of 'x’
x = x + 10 ;
cout<<"value of x in calc function is“<< x;
}
int main()
{ Output:
int x = 10; value of x in calc function
calc(x); // this will print the value of 'x’ is 20
cout<<"\nvalue of x in main is“<<x; value of x in main is 10
return 0;
}
#include <iostream>
using namespace std;
void swap(int a, int b) { //here a and b are formal parameters
int temp;
temp = a;
a=b;
b=temp;
cout<<"\nAfter swapping inside Swap:\n ";
cout<<"a = "<<a;
cout<<"\tb = "<<b;
return;
}
int main()
{
int a,b;
cout<<"Enter the two numbers to be swapped: "; cin>>a>>b;
cout<<"a = "<<a;
cout<<"\tb = "<<b;
swap(a,b);
cout<<"\nAfter swapping inside Main:\n ";
cout<<"a = "<<a;
cout<<"\tb = "<<b;
}
Output:

• Enter the two numbers to be swapped: 3 2


•a=3b=2
• After swapping inside Swap:
•a=2b=3
• After swapping inside Main:
•a=3b=2
Call by reference

• In call by reference, the address of the variable is passed into the function
call as the actual parameter.
• The value of the actual parameters can be modified by changing the formal
parameters since the address of the actual parameters is passed.
• In call by reference, the memory allocation is similar for both formal
parameters and actual parameters. All the operations in the function are
performed on the value stored at the address of the actual parameters, and
the modified value gets stored at the same address.
• #include <iostream>
• #include <string>
• using namespace std;
• void swap(int &a, int &b){
• int temp = a;
• a = b;
• b = temp;
• }
• int main()
• {
• int a,b;
• cout<<"Enter the two numbers to be swapped: "; cin>>a>>b;
• cout<<"a = "<<a;
• cout<<"\tb = "<<b;
• swap(a,b);
• cout<<"\nAfter swapping inside Main:\n ";
• cout<<"a = "<<a;
• cout<<"\tb = "<<b;
Output:

• Enter the two numbers to be swapped: 25 50


• a = 25 b = 50
• After swapping inside Main:
• a = 50 b = 25
No. Call by value Call by reference
1 A copy of the value is passed into the An address of value is passed into the
function function

2 Changes made inside the function is Changes made inside the function validate
limited to the function only. The outside of the function also. The values of
values of the actual parameters do not the actual parameters do change by changing
change by changing the formal the formal parameters.
parameters.
3 Actual and formal arguments are Actual and formal arguments are created at
created at the different memory the same memory location
location
Pass by Pointer
• #include <iostream>
• #include <string>
• using namespace std;
• void swap(int *a, int *b)
• {
• int temp = *a;
• *a = *b;
• *b = temp;
• }
Pass by Pointer
• int main()
• {
• int a,b;
• cout<<"Enter the two numbers to be swapped: "; cin>>a>>b;
• cout<<"a = "<<a;
• cout<<"\tb = "<<b;
• swap(a,b);
• cout<<"\nAfter swapping inside Main:\n ";
• cout<<"a = "<<a;
• cout<<"\tb = "<<b;
• }
Output:

• Enter the two numbers to be swapped: 23 54


• a = 23 b = 54
• After swapping inside Main:
• a = 54 b = 23
Default Parameters
• #include <iostream>
• #include <string>
• using namespace std;
• int mathoperation(int a, int b = 3, int c = 2){
• return ((a*b)/c);
• }
• int main()
• {
• int a,b,c;
• cout<<"Enter values for a,b and c: "; cin>>a>>b>>c;
• cout<<endl;
• cout<<"Call to mathoperation with 1 arg : "<<mathoperation(a);
• cout<<endl;
• cout<<"Call to mathoperation with 2 arg : "<<mathoperation(a,b);
• cout<<endl;
• cout<<"Call to mathoperation with 3 arg : "<<mathoperation(a,b,c);
• cout<<endl;
Output:

• Enter values for a,b and c: 10 4 6


• Call to mathoperation with 1 arg: 15
• Call to mathoperation with 2 arg: 20
• Call to mathoperation with 3 arg: 6
• As shown
Inline Functions

• An inline function is a function that expanded in line when it is invoked.


• That is the compiler replaces the function call with the corresponding
function code .
• Syntax:
• inline function-header { Function body }
Using Structure in function example:
• #include <iostream>
• #include <string>
• using namespace std;
• struct PersonInfo
• {
• int age;
• char name[50];
• double salary;
• };
• void printStructInfo(PersonInfo p)
• {
• cout<<"PersonInfo Structure:";
• cout<<"\nAge:"<<p.age;
• cout<<"\nName:"<<p.name;
• cout<<"\nSalary:"<<p.salary;
• }
• int main()
• {
• PersonInfo p;
• cout << "Enter name: ";
• cin.get(p.name, 50);
• cout << "Enter age: "; cin >> p.age;
• cout << "Enter salary: "; cin >> p.salary;
• printStructInfo(p);
• }
• Output:
• Enter name: Vedang
• Enter age: 22
• Enter salary: 45000.00
• PersonInfo Structure:
• Age:22
• Name: Vedang
• Salary:45000

You might also like