0% found this document useful (0 votes)
8 views3 pages

Lesson 17 Using Functions

Uploaded by

2mmukhavhuli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Lesson 17 Using Functions

Uploaded by

2mmukhavhuli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lesson 17 Using Functions

PART 1: WRITTING AND CALLING A FUNCTION

//Defining a function || Function definition

four parts of function definition

1. return type void || int || float etc.


2. the name of the function print || add || greetWithNames etc.
3. parameter list ()
4. body of the function {}

I.E

void foo ()........................ function header


{

//whatever statement you want executed

.......................................................

void foo()
{
cout << "Hellow , world!\n";

int main ()

{
// call our function

..............................................................
..............................................................
PART 2: FUNTIONS THAT CAN ACCEPT ARGUMENTS

#include <iostream>
#include <string>
using namespace std;

//They can accept arguments


// they require parameters

void foo (string s)


{
cout << "Hello, " << s << "\n";

int main()
{
// call the function and pass it a string "argument"
// a literal or a variable can be an argument

foo("Marvin"); // "Marvin" gets assigned to s in the function above

cin.get();//pause the program


}

//Another example would be one that accepts two or more integers as its only
parameters

void foo(int x, int y)


{
cout << (x + y) << endl;
}

int main ()
{
// call foo and passs it two integer arguments
foo (8, 3);// 8 will be passed to x and 3 to y and therefore execute the code
in the body function as addition
}

...................................................................................
...................................................................................
...................................................................................
......................................................
EXCERCISES STRUCTURE

RANDOM GENERATOR GAME

Libraries to include
#include <ctime>
#include <cstdlib>

//declare variables (including error, absError, randomisedNum, bestGuess, guess);

//Generate random number using srand(time(0)); function


//include a rand() function, initialized to randomisedNum and its parameters

//input user guess and store it

//initialize error to the max parameter and bestGuess to the lowest parameter

//Repeatedly input guesses until a sentinel is entered using a while loop


//follow the while loop with keeping track of bestGuess and smallest error by
initializing absError to an abs() function that has (randomizedNum - guess) as
parameters
//an if statement with (absError < error) as argument
//the body of the if statement must initialize error to absError and bestGuess to
guess
//display the difference and the randomizedNum

//input the next guess and store it


// display the result

You might also like