Community
Functions - Basic Concept
Today, we will see yet another concept present in any programming
language. Naturally, we will see why at all we need functions. How a
function is declared and defined in C++? What new terms are related
with functions which are not there in C?
First, we will look at the role of function. Suppose, you want to delegate a task then you will not like to tell each time how to perform that task. Instead, you will tell it once to the person whom you are delegating the task and whenever, you want the task to be done then you will simply call the person to do the task again. The role of function is similar to this situation only. If you have some logic to be repeated then you will put this logic in a function and call this function wherever required. Obviously, this do away repitition and makes the code more maintainable.
Now, let us see how to declare a function. The syntax for function declaration is:
data_type function_name(argument_declaration_list);
where,
data_type returned can be any valid data type we have seen in previous posts, for e.g., int, double, char, int * etc.
If a function returns 'void' then it is more appropriately called a pure procedure.
argument_declaration_list is of the form:
data_type1 param1, data_type2 param2,..data_typeN paramN
where again data_type1..N can be any valid data type.
In C++, arguments can be passed to functions using any of the following 3 ways:
First, we will look at the role of function. Suppose, you want to delegate a task then you will not like to tell each time how to perform that task. Instead, you will tell it once to the person whom you are delegating the task and whenever, you want the task to be done then you will simply call the person to do the task again. The role of function is similar to this situation only. If you have some logic to be repeated then you will put this logic in a function and call this function wherever required. Obviously, this do away repitition and makes the code more maintainable.
Now, let us see how to declare a function. The syntax for function declaration is:
data_type function_name(argument_declaration_list);
where,
data_type returned can be any valid data type we have seen in previous posts, for e.g., int, double, char, int * etc.
If a function returns 'void' then it is more appropriately called a pure procedure.
argument_declaration_list is of the form:
data_type1 param1, data_type2 param2,..data_typeN paramN
where again data_type1..N can be any valid data type.
In C++, arguments can be passed to functions using any of the following 3 ways:
- Call-By-Value
- Call-By_Reference using Pointer Variable
- Call-By-Reference using Reference Variable
In call-by-value, argument value is not altered in calling program but if passed using call-by-reference then changes done to the value of argument in the called function also gets reflected in the calling function.In natural sense, call-by-value is like giving a photocopy of a paper to your friend so that he can prepare a gist of it. Now, you are only interested in gist and not on what he does to that paper - he may write on it or do underlining. However, call-by-reference is like giving paper to your friend to do any corrections on the paper itself.
Now, question is when to use call-by-reference using pointer variable and when by reference variable. First, let us understand how these 3 techniques work.
- In call-by-value, the formal parameter of the function gets a copy of the actual agrument. As such, changes to passed variable is not reflected in the calling program.
- In calling using pointer, the address of the variable is passed to the called function and therefore, any changes done are reflected in calling program as well.
- Reference variable is nothing but an alias for a variable. In terminology of compilers, the symbol table will have two entries for each name and addresses of these variables will be same. Again, therefore, changes to variable in called program are visible in the calling program.
Now, you must be wondering how to decide when to use call-by-reference using pointer and when to use call-by-reference using reference variable. Here are rules that help in making this decision:
- Call-By-Value: Used for small & non-modifiable arguments
- Call-By-Reference Using Pointers:
- Increased Performance: No overhead of copying large data
- Weaker Security: Caller's data can be corrupted
- Weaker Security: Caller's data can be corrupted
- Call-By-Reference Using Reference Variables:
- Increased Performance for same reason as above
- Data is secure
Now, let us see how functions are declared, defined, and used in each of the 3 cases:
- Data is secure
Now, let us see how functions are declared, defined, and used in each of the 3 cases:
- Call-By-Value
In Calling Program:
int a = 5;
int b = 6;
swap(a,b);
Function Declaration:
void swap(int a,int b); or simply
void swap(int,int);
Function Definition:
void swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
- Call-By-Reference using Pointers
In Calling Program:
int a=5,b=6;
swap(&a,&b);
Function Declaration:
void swap(int *a,int *b); or simply,
void swap(int *,int *);
Function Definition:
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
}
int a=5,b=6;
swap(&a,&b);
Function Declaration:
void swap(int *a,int *b); or simply,
void swap(int *,int *);
Function Definition:
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
}
- Call-By-Reference using Reference Variables:
In Calling Program:
int a=5,b=6;
swap(a,b);
Function Declaration:
void swap(int &a, int &b);
Function Defintion:
void swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
Points to Ponder
int a=5,b=6;
swap(a,b);
Function Declaration:
void swap(int &a, int &b);
Function Defintion:
void swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
}
Points to Ponder
- Each of the 3 cases have different function declaration. In call-by-value, for argument list, we have data-type followed by variable name. In call-by-reference using pointers, we have dereferencing operator (*) in-between data-type and variable name. In call-by-reference using reference variable, we have address-of (&) operator in-between data-type and variable name.
- Just by looking in the calling program, you cannot tell whether call-by-value is used or call-by-reference using reference variable. However, if call-by-reference using pointers is used then you will have (&) in-between data-type and variable name in the calling program.
- Call-by-reference using reference variable has neatier code than inside function using call-by-reference using pointers .
- Most importantly, in the first case i.e. in call-by-value, the variables in the calling program will not be swaped but in the last two cases they will do for the reasons we have learned earlier in this post.
C++ adds many more concepts to functions like default arguments, inline functions, overloaded functions, over-ridden function, virtual function, etc.
We will see these concepts in next post.
Advertisement
Page Author
From Here You Can…
Information
- 1840 Views
- 0 Comments
Most Recent Related Content
- Lesson
- Avatar

- Title
- Tips For How To Enjoy Participating in a Voicethread
- Body
- To be an active Voicethreader, start by carefully working your way through a ...
- Author
- Lesson
- Avatar

- Title
- PHP: Array Pagination
- Body
- This is a very easy to install pagination class that takes a PHP array and pa...
- Author
- Lesson
- Avatar

- Title
- Tests and Discussion Questions - What's the Difference?
- Body
- “Virtually all learning difficulties that children face are caused by adults’...
- Author
- Presentation
- Avatar

- Title
- C programming (From Basics to Advanced Concepts)
- Description
- Friends, this presentation of 50 slides will help you to quickly revise right...
- Author
- Lesson
- Avatar

- Title
- PHP: Simple Guestbook Tutorial
- Body
- The purpose of this tutorial is to show you how to create a very simple g...
- Author
- Lesson
- Avatar

- Title
- Difference between c and c++
- Body
- C does not have any classes or objects. It is procedure and function driven. ...
- Author
- Lesson
- Avatar

- Title
- PHP: File Handling
- Body
- When working with files in PHP you must be careful, this can be very dangerou...
- Author
- Lesson
- Avatar

- Title
- electronics
- Body
- Author
Published In…
Computer Science
Software Carpentry
Java
Agile Software Development
Everything PHP
Web 2.0
Technical Writing, Training, Publishing
Open Source
tech
Game Development
Computer Basics
PHP and MySQL
Google
J2EE
Computer Hardware
GNU & Linux
Social Media
C++
Programming Languages
Software Architecture
Computer Science Learners
singh's community
This work is public domain.