Introducing C++ - Functions
This tutorial intends to teach beginners how to use functions in programs along with the various terms used like 'calling' a function and 'passing' values to a function and so on.
AI
Ringkasan AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Kode Sumber
<p align="center"><b><font color="#000080" size="6">Introducing C++ - Part 3</font></b></p>
<p align="center"> </p>
<p align="center"><font size="6" color="#0000FF"><b>Functions</b></font></p>
<p align="center"> </p>
<p align="left"><b><font size="6" color="#0000FF">
</font></b><font size="4" color="#0000FF">W</font><font size="4">e will now see
the usage of functions is C++. Functions are very useful if you desire to use a
particular set of statements or a particular operation again and again. With the
help of a simple example i will explain the concept of functions. </font></p>
<p align="left"><font size="4">The general form of a function is</font></p>
<p align="left"><font size="4"> <i>return-type</i>
function-name (<i>parameter 1</i>, <i>parameter 2)</i></font></p>
<p align="left"><font size="4"> {</font></p>
<p align="left"> </p>
<p align="left"><font size="4"> }</font></p>
<p align="left"><font size="4"><i>
return-type -> </i>Its the value that the function will
return after various calculations. </font></p>
<p align="left"><font size="4">
function-name -> This is the name of the function that we like to use</font></p>
<p align="left"><font size="4"> <i>
parameter 1</i> -> This is also called as the argument.
These are the variables that are being passed on to the function</font></p>
<p align="left"><font size="4"> <i>
parameter 2</i> -> Another variable that is passed to
the function</font></p>
<p align="left"><font size="4"> I will
illustrate the use of functions with the help of the following example</font></p>
<p align="left"><i><font size="4">#include<iostream.h></font></i></p>
<p align="left"><i><font size="4">int add(int x, int y)</font></i></p>
<p align="left"><i><font size="4">{</font></i></p>
<p align="left"><i><font size="4">int z;</font></i></p>
<p align="left"><i><font size="4">z=x+y;</font></i></p>
<p align="left"><i><font size="4">return z;</font></i></p>
<p align="left"><i><font size="4">}</font></i></p>
<p align="left"><i><font size="4">void main()</font></i></p>
<p align="left"><i><font size="4">{</font></i></p>
<p align="left"><i><font size="4">int a=3,b=5,c;</font></i></p>
<p align="left"><i><font size="4">c=add(a,b);</font></i></p>
<p align="left"><i><font size="4">cout<<"The sum is "<<c;</font></i></p>
<p align="left"><i><font size="4">}</font></i></p>
<p align="left"> </p>
<p align="left"><font size="4">Let us see how the code works</font></p>
<p align="left"><i><font size="4">int add(int x, int y)</font></i></p>
<p align="left"><i><font size="4">{</font></i></p>
<p align="left"><i><font size="4">int z;</font></i></p>
<p align="left"><i><font size="4">z=x+y;</font></i></p>
<p align="left"><i><font size="4">return z;</font></i></p>
<p align="left"><i><font size="4">}</font></i></p>
<p align="left"> <font size="4"> In the
above code segment 'add' is the function name and 'int' is the return-type.
</font></p>
<p align="left"><font size="4"> There are
two parameters 'int x' and 'int y'.</font></p>
<p align="left"><font size="4"> The variable
int z is used to store the sum of the two numbers.</font></p>
<p align="left"><font size="4"> As
mentioned before,</font></p>
<p align="left"><i><font size="4">
return z;</font></i></p>
<p align="left"><font size="4">
the integer z is returned back. </font></p>
<p align="left"> </p>
<p align="left"><font size="4">
Ok but where does the terms 'calling' and 'returning' the values arises? We will
see that...</font></p>
<p align="left"><font size="4"> In the
main section you will be able to the lines</font></p>
<p align="center"><i><font size="4">int a=3,b=5,c;</font></i></p>
<p align="left"><font size="4"> Here
we are declaring (and initializing) a and b and declaring a variable c.</font></p>
<p align="left"><font size="4">
Consider the following line:</font></p>
<p align="center"><i><font size="4">c=add(a,b);</font></i></p>
<p align="left"><font size="4"> At
this point the function gets 'CALLED'. The function 'add' is called with two
parameters 'a' and 'b'. It is said that the integers a and b are passed to the
function 'add'. </font></p>
<p><font size="4"> When the function
is called the program control is transferred to the 'add' function. The two
parameters a and b are added and 'RETURNED' through the variable 'z'. This
variable is stored in 'c' and then printed.</font></p>
<p><font size="4"> This is how
functions work in C++ and any other language. This is just one way of using
functions and you will know more as you proceed.</font></p>
<p align="center"><font size="4">RATE THE TUTORIAL IF YOU LIKE IT and GIVE YOUR
COMMENTS TOO.</font></p>
<p align="center"><font size="4">THANK YOU!</font></p>
Komentar Asli (3)
Dipulihkan dari Wayback Machine