In this lesson I will show you how to write and call a function in order to\u00a0 \u00a0simplify the structure of any program.<\/p>\n
Creating a function:<\/strong><\/p>\n A function in C++, a function is a group of statements that is given a name, and which can be called from some point of the program. The most common syntax to define a function is:<\/p>\n Type name (parameter1, parameter2,) {statements}<\/em><\/p>\n Where:<\/p>\n –type<\/em> <\/strong>is the type of value returned by the function.<\/p>\n –name<\/em> <\/strong>is the identifier by which the function can be called.<\/p>\n –parameters<\/em> <\/strong>(as many as needed): 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 which 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.<\/p>\n – Statements<\/em><\/strong> is the function’s body. It is a block of statements surrounded by braces { } that specify what the function actually does.<\/p>\n \u00a0<\/strong><\/p>\n Types of functions:<\/strong><\/p>\n System-Supplied Functions:<\/u><\/p>\n These are available to anyone who writes a C++ program. This saves the programmer\u2019s time from writing own functions. They are completely debugged, efficient and always produce a precise output. The important contribution of the system-supplied functions is that they provide clarity to the program because they do not have to be redefined. It reduces the source code, which saves time of the programmer.<\/p>\n User-Supplied Functions:<\/u><\/p>\n C++ language allows additional functions besides the built-in functions called the user-defined function. It allows programmers to define their own functions. The programmer must code the logic of this type. In order to do so, a declaration is needed.<\/p>\n Calling a function:<\/strong><\/p>\n While creating a C++ function, you give a definition of what the function has to do. To use a function, you will have to call or invoke that function.<\/p>\n When a program calls a function, program control is transferred to the called function. A called function performs defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.<\/p>\n To call a function, you simply need to pass the required parameters along with function name, and if function returns a value, then you can store returned value.<\/p>\n This is an example of a code where I created a function and then I called it into my main.<\/p>\n <\/p>\n And this is how the program runs:<\/p>\n <\/a><\/p>\n Any doubt please leave it in comments and I will be glad to answer.<\/p>\n -The admin.<\/p>\n <\/a> <\/p>\n","protected":false},"excerpt":{"rendered":" In this lesson I will show you how to write and call a function in order to simplify the structure of any program. Creating a function: A function in C++, a function is a group of statements that is given… Continue Reading →<\/a><\/p>\n","protected":false},"author":259,"featured_media":24625,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[582,238,128,129,206,62,614,615],"_links":{"self":[{"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/posts\/24627"}],"collection":[{"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/users\/259"}],"replies":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/comments?post=24627"}],"version-history":[{"count":1,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/posts\/24627\/revisions"}],"predecessor-version":[{"id":24628,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/posts\/24627\/revisions\/24628"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/media\/24625"}],"wp:attachment":[{"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/media?parent=24627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/categories?post=24627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/tags?post=24627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\/\/Function that calculates the area of a rectangle<\/span>\n#include <iostream><\/span>\nusing<\/span> namespace<\/span> std;<\/span>\n\n\/\/Declaration of what the function does<\/span>\nvoid<\/span> area<\/span>(){<\/span>\nint<\/span> l=<\/span>0<\/span>,<\/span> w=<\/span>0<\/span>;<\/span>\ncout<<<\/span>\"Please enter length and width: \"<\/span><<endl;<\/span>\ncout<<<\/span>\"Length: \"<\/span>;cin>>l;<\/span>\ncout<<<\/span>\"Width: \"<\/span>;cin>>w;<\/span>\ncout<<<\/span>\"The area is: \"<\/span><<<\/span> l<\/span> *<\/span> w<<endl;<\/span>\n}<\/span>\n\nint<\/span> main<\/span>(){<\/span>\ncout<<<\/span>\"I will calculate the area of a rectangle\"<\/span><<endl;<\/span>\narea();<\/span>\/\/Calling the function inside the execution of the program<\/span>\n\n}<\/span>\n<\/pre>\n<\/div>\n