0% found this document useful (0 votes)
20 views

Pseudocode Functions

The document explains pseudocode as a method for outlining algorithms in plain English without programming syntax, serving as a planning tool before coding. It also covers user-defined functions, including their syntax, declaration, and implementation, along with examples in C and C++. Additionally, the document introduces recursion and provides an example of a factorial function, illustrating how recursive functions work.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Pseudocode Functions

The document explains pseudocode as a method for outlining algorithms in plain English without programming syntax, serving as a planning tool before coding. It also covers user-defined functions, including their syntax, declaration, and implementation, along with examples in C and C++. Additionally, the document introduces recursion and provides an example of a factorial function, illustrating how recursive functions work.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

pseudocode

- a program’s algorithm written in plain english to provide an


outline of the execution flow of statements.

- does not use any programming language syntax; use short


phrases and mathematical notations.

- omits programming implementation details e.g. no variable


declarations.

- used as a tool for planning and to understand the program’s


structure before the actual coding.
-
example 1
pseudocode to check if the entered number by user is not a 5 or a 6.

read number

If (number = 5)

write "your number is 5"

else if (number = 6)

write "your number is 6"

else

write "your number is not 5 or 6“


https://www.qacps.org/cms/lib02/MD01001006/Centricity/Domain/847/Pseudo_Code%20Practice_Proble ms.pdf
example 2
pseudocode that will count all the even numbers up to a user
defined stopping point.

read count

set i to 0

while (i < count)

set even to even + 2

i = i + 1

write even
https://www.qacps.org/cms/lib02/MD01001006/Centricity/Domain/847/Pseudo_Code%20Practice_Proble ms.pdf
user-defined functions
programmers can define their own functions (procedures, sub-
routines)

- a function is group of statements that perform a specific task.

syntax

function type function name( parameter list )


{
local-definitions;
function implementation (statements);
}

- a function has two parts: a header and a function body.


user defined functions cont’d

If the function returns a value then the type of that value must be
specified in function type(return type).

If the function does not return a value then the function


type must be void.

function name : same rules apply as case of identifiers.

parameter list: lists the formal parameters of the function


together with their types.
user defined functions cont’d
function signature : function name & parameter list.

function declaration : return type, function name &


parameter list.

local-definitions
- definitions of variables that are used in the function-
implementation only (no meaning outside function).

function-implementation :statements that are executed


by the function.

function prototype : a function is declared and used in a program


before the function is actually defined.
example: sum function
#include <iostream>
using namespace std;
int sum (int a, int b)
{
int add;
add = a+b;
return add;
}
int main ()
{
int c;
c = sum (10,20);
cout << “the sum is " << c; }
tutorial
using function prototyping to write a program to
find the largest/highest of two numbers.

re-write the a program from C to C++.


highest value of two
#include <stdio.h>
// function declaration
int highest(int num1, int num2);

int main ()
{
int a = 1000, b = 1001, answer;
// calling a function
answer = highest(a, b);

printf("highest value is : %d\n", answer );


return 0;
}
highest value of two
/* function returning the highest between two
numbers */

int highest(int num1, int num2)


{
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Recursion
“(mathematics, computing) the practice of describing
numbers, expressions, etc. in terms of the numbers,
expressions, etc. that come before them in a series:”
– Cambridge dictionary

• in a recursive process the solution to a problem


depends on solutions to smaller occurrences of the
same problem.

• recursive function : a function that calls itself


repeatedly until a terminating point is reached.
example: factorial (pseudocode)

function factorial is:

input: integer n such that n >= 0

output: [n × (n-1) × (n-2) × … × 1]

1. if n is 0, return 1

2. otherwise, return [ n × factorial(n-1)]

end factorial

https://en.wikipedia.org/wiki/Recursion_(computer_science)
example: factorial

n is 4 = 4 * f3
= 4 * (3 * f2)
= 4 * (3 * (2 * f1))
= 4 * (3 * (2 * (1 * f0)))
= 4 * (3 * (2 * (1 * 1)))
= 4 * (3 * (2 * 1))
= 4 * (3 * 2)
= 4 * 6
= 24
example: factorial
#include <iostream>
using namespace std;
long factorial (long a){
if (a > 1)
return (a * factorial(a-1));
else
return 1;
}

int main (){


long number = 10;
cout << number << "! = " << factorial (number);
return 0;
}

You might also like