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

Q4 ICT Reviewer

Functions allow programmers to organize code into reusable subprograms. There are two types of functions: standard functions that are predefined and programmer-defined functions that are created by programmers. Functions are called by their name and can optionally take parameters. Arrays allow storing multiple values in a single variable. One-dimensional arrays store values in an indexed list while two-dimensional arrays store one-dimensional arrays. Strings are character arrays that hold text. Useful string functions allow manipulating strings, such as getting length, copying, concatenating, converting case, comparing, and more.

Uploaded by

Martin Recentes
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)
99 views3 pages

Q4 ICT Reviewer

Functions allow programmers to organize code into reusable subprograms. There are two types of functions: standard functions that are predefined and programmer-defined functions that are created by programmers. Functions are called by their name and can optionally take parameters. Arrays allow storing multiple values in a single variable. One-dimensional arrays store values in an indexed list while two-dimensional arrays store one-dimensional arrays. Strings are character arrays that hold text. Useful string functions allow manipulating strings, such as getting length, copying, concatenating, converting case, comparing, and more.

Uploaded by

Martin Recentes
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/ 3

4TH QUARTER

REVIEWER IN COMPUTER SCIENCE


1. Functions
- are subprograms that perform specific tasks
- make your program simpler, more
manageable and easier to understand
1.1. Types of Functions
1.2.1. Standard Functions
- are predefined or built-in functions in
compilers
- e.g. main(), printf(), scanf()
1.2.2. Programmer-Defined Functions
- are
functions
created
by
programmers*
1.2. *Creating Functions
fType fName(pType pName, ...)
{
<code>
return;
}
function header
function body
fType datatype returned by the function
fName function name
pType parameter datatype
pName parameter name
Note: functions can be created before or after
main() and/or other functions. However,
they should be called beforehand.
Example 1.2.1. (Parameter-Passing Function)
...
int sum(int addend1, int
addend2)
{
return addend1 + addend2;
}
int main()
{
int a, b;
scanf(%d\n%d, a, b);
printf(%d, sum(a, b));
...
Example 1.2.2. (Parameter-Passing Function)
...
int sum(int addend1, int
addend2);
int main()
{

int a, b;
scanf(%d\n%d, a, b);
printf(%d, sum(a, b));
getch();
return 0;
}
int sum(int addend1, int
addend2)
{
return addend1 + addend2;
}
...
A line of code is inserted before main().
This program prints a set of 20 asterisks.
Example 1.2.3. (Passing No Value, Revision)
...
void asterisks(void)
{
printf(*);
}
int main()
{
int i;
for(i = 0; i < 20; i++)
{
asterisks();
}
...
1.3. *Calling Functions
fName(param);
fName function name
param - parameters
2. Arrays
- store large amounts of data in one variable
- could only hold homogenous data
- arrays that store characters are called
strings**
2.1. One-Dimensional Arrays
Syntax:
aType aName[size] = {<data>};
aType array datatype
aName array name
size array size (e.g. 4)
Example 2.1.1. (Array Declaration)

4TH QUARTER
REVIEWER IN COMPUTER SCIENCE

...
int array1[10] = {1, 4, 5, 7,
3, 5, 7, 9, 0, 2};
...

for(j = 0; j < 4; j++)


{
printf(%d,
array1[i][j]);
}

Graphical Representation 2.1.1.

1
0

4
1

5
2

7
3

3
4

5
5

7
6

9
7

0
8

2
9

array index = n - 1
Example 2.1.2. (Array Execution)
...
int main()
{
int array1[10] = {1, 4, 5,
7, 3, 5, 7, 9, 0, 2};
int i;
for(i = 0; i < 10; i++)
{
printf(%d\n,
array1[i]);
}
...
This program prints all contents of array1,
from index 0 to index 9.
2.2. Two-Dimensional Arrays
- store one-dimensional arrays within arrays
Syntax:
aType aName[size1][size2];

}
...
This program prints 12 user-input integers
stored in array1.
Graphical Representation 2.2.1.
x
0
1
array1[0][x]
2
3
array1[1][x]
15
0
array1[2][x]
1
4
assumed data

2
5
21
8

3
6
8
7

3. **Strings
- are character arrays
- hold groups of characters in the form of
words, phrases, sentences, etc.
- string sizes should be one more than the
number of characters it should hold (n + 1)
for the \0, or the null byte.***
Syntax:
char sName[size + 1];
This string should hold size characters.
Example 3.0.

This array stores size1 arrays of size2


elements each.

...
char string1[9] = Hello!;

Example 2.2.1.
...
int main()
{
int array1[3][4];
int i, j;

printf(%s, string1);
...

for(i = 0; i < 3; i++)


{
for(j = 0; j < 4; j++)
{
scanf(%d,
&array1[i][j]);
}
}
for(i = 0; i < 3; i++)
{

%s is an operator for strings.


Graphical Representation 3.0.
H e l l o ! \0
0
1
2
3
4
5
6
string index = n 1
***\0 terminates the string
G garbage value

G
7

G
8

3.1. String Functions


- a special library string.h contains useful
functions for string manipulation
Syntax:

4TH QUARTER
REVIEWER IN COMPUTER SCIENCE

...
#include<string.h>
...
3.1.1.

gets(strName);
- reads user-input characters then
stores them to strName
- similar
to
scanf();
and
fgets();
3.1.2. puts(strName);
- displays the contents of strName
- similar to printf();
3.1.3. strlen(strName);
- counts the number of characters
strName is currently holding
- returns and integer
- the string terminator, or \0, is
ignored
3.1.4. strcpy(strDest, strSrc);
- copies all contents of strSrc then
stores them to strDest
3.1.5. strncpy(strDest,
strSrc,
size);
- copies partial contents of strSrc
(at size size) then stores them to
strDest
3.1.6. strcat(strDest, strSrc);
- appends strSrc to strDest
3.1.7. strlwr(strName);
- converts all contents of strName
to its lowercase form
3.1.8. strupr(strName);
- converts all contents of strName
to its uppercase form
3.1.9. strrev(strName);
- reverses all characters in strName
3.1.10. strcmp(strName1, strName2);
- compares
strName1
and
strName2, then returns either -1,
0 or 1, depending on which comes
first alphabetically
- returns -1 if strName1 comes first
before strName2 alphabetically
- returns 0 if no one comes first
- returns 1 if strName2 comes first
before strName1 alphabetically
- doesnt ignore capital letters;
ASCII-based

3.1.11. strcmpi(strName1,
strName2);
- similar to strcmp(strName1,
strName2);, but ignores letter
cases
3.1.12. toupper(letter);
- converts letter to its uppercase
form
3.1.13. tolower(letter);
- converts letter to its lowercase
form.

Keith Badulis

You might also like