Programming in C++ by Laxmisha Rai
Author:Laxmisha Rai
Language: eng
Format: epub
Publisher: Walter de Gruyter
Published: 2019-02-15T00:00:00+00:00
10.3 Arguments and parameters
In the previous programs, we used only functions of type void. That means, these functions do not return anything to main(), instead they perform the task required. However, this is not the case while using functions in general. Usually, functions return a value after performing some operations. Program 10.4 illustrates an example function with parameters and return value.
// Program 10.4 // Function with parameters #include <iostream> using namespace std; int find_square(int temp) { ââââreturn temp * temp; } int main() { ââââint num, square_num; ââââcout << " Enter a number to be squared " << endl; ââââcin >>num; ââââsquare_num = find_square(num); ââââcout << " Square of " << num << " is equal to " âââââââ<< square_num << endl; ââââreturn 0; }
The result of Program 10.4 is as follows:
Enter a number to be squared 18 Square of 18 is equal to 324
Program 10.4 is an example function where the function returns an integer to the main(). In this program, the objective is to compute the square of number (num) using a function with name find_square(), and returning the computed square to main(). In this case, the parameter used in the function code is temp, which is of integer type. When a function is declared the number of arguments passed to the function and their names must also be indicated. The name chosen for an argument is called its formal parameter name. Formal parameters must be declared inside a function before they are used in the function body. Always note that variables defined inside a function are known as automatic variables since they are automatically created each time the function is called and are destroyed once the function is executed. Their values are local to the function; they can be accessed only inside the function in which they are defined and not by other functions.
In Program 10.4, the num variable in the main() is called argument, and the temp in find_square() is called as parameter. Some authors use parameters and arguments interchangeably. However, the arguments are values passed into a function call, and parameters are variables defined in the function to receive them. In some sources, there are definitions for actual argument and formal parameter. The formal parameter is what is in the function declaration/definition/prototype; the actual argument is what is passed when calling the function, an instance of a formal parameter. In program shown in Fig. 10.2, the num is the actual argument, and the temp is the formal parameter. Each parameter consists of a type followed by an identifier, with each parameter being separated from the next by a comma. Each parameter looks very much like a regular variable declaration (for example: int x), and in fact acts within the function as a regular variable that is local to the function. The purpose of parameters is to allow passing arguments to the function from the location where it is called from.
Fig. 10.2: Terminologies related to functions. It is very important to remember that arguments and parameters must match with the data type, and more than one argument or parameters can be separated by commas.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6831)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6561)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6427)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4316)
Functional Programming in JavaScript by Mantyla Dan(4038)
Solidity Programming Essentials by Ritesh Modi(3998)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3789)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3734)
The Ultimate iOS Interview Playbook by Avi Tsadok(3708)
