I’m sure my code can be optimized but I tried to write it as clean i.e. readable, as possible. So yeah, all I did was print some options for the user and ask for two numbers to work with. Finally, I created some functions so they are called according to the user’s needs.
This is ontofunctions.py in action!
]]>Creating a function:
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:
Type name (parameter1, parameter2,) {statements}
Where:
–type is the type of value returned by the function.
–name is the identifier by which the function can be called.
–parameters (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.
– Statements is the function’s body. It is a block of statements surrounded by braces { } that specify what the function actually does.
Types of functions:
System-Supplied Functions:
These are available to anyone who writes a C++ program. This saves the programmer’s 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.
User-Supplied Functions:
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.
Calling a function:
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.
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.
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.
This is an example of a code where I created a function and then I called it into my main.
//Function that calculates the area of a rectangle #include <iostream> using namespace std; //Declaration of what the function does void area(){ int l=0, w=0; cout<<"Please enter length and width: "<<endl; cout<<"Length: ";cin>>l; cout<<"Width: ";cin>>w; cout<<"The area is: "<< l * w<<endl; } int main(){ cout<<"I will calculate the area of a rectangle"<<endl; area();//Calling the function inside the execution of the program }
And this is how the program runs:
Any doubt please leave it in comments and I will be glad to answer.
-The admin.
]]>Creating a function:
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:
Type name (parameter1, parameter2,) {statements}
Where:
–type is the type of value returned by the function.
–name is the identifier by which the function can be called.
–parameters (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.
– Statements is the function’s body. It is a block of statements surrounded by braces { } that specify what the function actually does.
Types of functions:
System-Supplied Functions:
These are available to anyone who writes a C++ program. This saves the programmer’s 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.
User-Supplied Functions:
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.
Calling a function:
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.
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.
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.
This is an example of a code where I created a function and then I called it into my main.
//Function that calculates the area of a rectangle #include <iostream> using namespace std; //Declaration of what the function does void area(){ int l=0, w=0; cout<<"Please enter length and width: "<<endl; cout<<"Length: ";cin>>l; cout<<"Width: ";cin>>w; cout<<"The area is: "<< l * w<<endl; } int main(){ cout<<"I will calculate the area of a rectangle"<<endl; area();//Calling the function inside the execution of the program }
And this is how the program runs:
Any doubt please leave it in comments and I will be glad to answer.
-The admin.
]]>(The credit of the image goes to https://flic.kr/p/4C37pr)
Welcome to this special post.
As you can see, it is a quiz.
Here are my codes:
]]>(The credit of the image goes to https://flic.kr/p/4C37pr)
Welcome to this special post.
As you can see, it is a quiz.
Here are my codes:
]]>Well back to business, in the first part of the quiz is make a function called “superpower” able to ask for two numbers and return the power of it on a long type. There’s a lot of methods you are able to, but the easiest one is with the library <cmath>, where you are able to call a function(wow) called ‘pow(x,y)’, that makes the power of two numbers, that’s it.
And for the second part, you have to make a function called stars where you input the value of the length of a chain of stars, as said previously theres a lot of methods but the easiest one is with a for( ), below i will paste a link to the code for the Quiz #06.
Thanks for reading.
github Code for Quiz #06
https://github.com/AndresSV/TC1017/blob/master/Quiz%20%2306%20.cpp
flickr photo by Craig Sunter
https://www.flickr.com/photos/16210667@N02/13973604274
Creative Commons Public Domain
And this is the program working:
If you want to get the code on Github here is the link:
https://github.com/Jocapear/TC1014/blob/master/WSQ08.py
This activity has to have the same result as WSQ03, but with functions. It was not hard to do this activity, I didn’t have big problems, just some little details. But I did have one which I wasn’t expecting: I used the shortcuts of the operations as the first 3 letters to keep it simple. Product was “pro”, for example, but the problem came with the division, because “div” is used in C++ as a command, so when I tried to run in, Terminal read it as a command instead of a variable. When I realized that, I changed the variable for “divv” so it becomes a variable again. It ran perfectly after that.
]]>This activity has to have the same result as WSQ03, but with functions. It was not hard to do this activity, I didn’t have big problems, just some little details. But I did have one which I wasn’t expecting: I used the shortcuts of the operations as the first 3 letters to keep it simple. Product was “pro”, for example, but the problem came with the division, because “div” is used in C++ as a command, so when I tried to run in, Terminal read it as a command instead of a variable. When I realized that, I changed the variable for “divv” so it becomes a variable again. It ran perfectly after that.
]]>