Chapter 5.0
Chapter 5.0
Lecturer :
Pn. Wan Fazlini Idayu binti Wan Fakari
Function and Array
5.1 Remember FUNCTION statements
5.1.1 Define FUNCTION statement
5.1.2 Identify the need for FUNCTION statements
5.1.3 Describe the structure of FUNCTION statements
5.7 Use ARRAY and FUNCTION statement to demonstrate the I/O operation
FUNCTION statements
Functions
• A function is a group of statements that together perform a
task.
• Every C program has at least one function, which is main(),
and most programs can define additional functions.
• You can divide up your code into separate functions, where
each function performs a specific task.
• A function declaration tells the compiler about a function's
name, return type, and parameters.
• A function definition provides the actual body of the
function.
5
Types of function
• There are two type of function:-
a. Predefined function
b. User-defined function
Types of function - Predefined function
• Predefined functions are functions that have been written and we can
use them in our C statements.
12
FUNCTIONS
• there are five types of functions and they are:
1. Functions with no arguments and no return
values.
2. Functions with arguments and no return values.
3. Functions with arguments and return values.
4. Functions that return multiple values.
5. Functions with no arguments and return values.
13
1. Functions with no arguments and no return value.
Reference: http://rajkishor09.hubpages.com/hub/Types-of-Function-in-C-Programming-Languages
15
1. Functions with no arguments and no return value.
#include<stdio.h>
#include<conio.h>
void printline()
{
int i;
printf("\n");
for(i=0;i<30;i++)
{
printf("-");
}
printf("\n");
}
void main()
{
clrscr();
printf("Welcome to function in C");
printline();
printf("Function easy to learn.");
printline();
getch();
} 16
Reference: http://rajkishor09.hubpages.com/hub/Types-of-Function-in-C-Programming-Languages
2. Functions with arguments and no return value.
Reference: http://rajkishor09.hubpages.com/hub/Types-of-Function-in-C-Programming-Languages
17
2. Functions with arguments and no return value.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
add(30,15);
add(63,49);
add(952,321);
getch();
}
18
Reference: http://rajkishor09.hubpages.com/hub/Types-of-Function-in-C-Programming-Languages
3. Functions with arguments and return value.
Reference: http://rajkishor09.hubpages.com/hub/Types-of-Function-in-C-Programming-Languages
19
3. Functions with arguments and return value.
#include<stdio.h>
#include<conio.h>
void main()
{
int z;
clrscr();
z = add(952,321);
printf("Result %d.\n\n",add(30,55));
printf("Result %d.\n\n",z);
getch();
}
20
Reference: http://rajkishor09.hubpages.com/hub/Types-of-Function-in-C-Programming-Languages
4. Functions with no arguments but returns value.
Reference: http://rajkishor09.hubpages.com/hub/Types-of-Function-in-C-Programming-Languages
21
4. Functions with no arguments but returns value.
#include<stdio.h>
#include<conio.h>
int send()
{
int no1;
printf("Enter a no : ");
scanf("%d",&no1);
return(no1);
}
void main()
{
int z;
clrscr();
z = send();
printf("\nYou entered : %d.", z);
getch();
}
22
Reference: http://rajkishor09.hubpages.com/hub/Types-of-Function-in-C-Programming-Languages
5. Functions that return multiple values.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=20, b=11, p,q;
clrscr();
calc(a,b,&p,&q);
printf("Sum = %d, Sub = %d",p,q);
getch();
}
23
Reference: http://rajkishor09.hubpages.com/hub/Types-of-Function-in-C-Programming-Languages
Declaring a function
return_type function_name ( formal_parameter_list );
• calculate();
– The return type and the formal parameters are not written.
– This function does not receive any parameter. It returns an integer value.
25
Defining a function
• The syntax of a function definition is:
function header
Example:
if given function defintion as below:
float avrg(int a, int b, int c)
{
return (a+b+c)/3.0;
}
Example:
if given function defintion as below:
void greeting(void)
{
printf("Hello");
return;
}
29
• Formal parameters are variables that are declared in the header of
the function definition
– Example:
greeting; // Error. greeting is a function.
//So, it must have the ()
// eventhough no parameter present
30
Figure : void function with parameters
31
Figure : Function that returns a value
32
Function that calls itself is known as recursive function
Example:
int factorial(int n)
{
if (n>1) return n * factorial(n-1);
return 1;
}
33
return statement
• A function returns a computed value back to
the calling function via a return statement.
35
Local & global variables
• Local variable is a variable declared inside a
function.
– This variable can only be used in the function.
36
Example: Local vs. Global
#include<stdio.h>
void print_number(void);
p is declared outside of all
int p; functions. So, it is a global
variable.
void main (void)
{
q is declared inside the
int q = 5;
function main. So, it is a local
printf(“q=%d”, q); variable to the function.
p=10;
print_number();
}
p can be used anywhere
void print_number(void)
{
Error! q can only be used in
printf(“%d”,p); the function main, because it
q = q + 5; is a local variable
37
}
Example: Local vs. Global
#include<stdio.h>
double compute_average (int num1, int num2);
void main (void) Same variable names?!?
{ --it’s OK; they’re local to
their functions. Compiler treat
double average; them as different variables.
int age1 = 18, age2 = 23;
average = compute_average(age1, age2);
return 0;
}
double average (int num1, int num2)
{
double average;
average = (num1 + num2) / 2.0;
return average;
} 38
Scope
• Scope determines the area of the program in
which an identifier is visible (ie. the identifier can
only be used in that area)
• Examples:
– Scope of a local variable : only in the function body
where t was declared.
– Scope of a global variable : everywhere in the program.39
Scope
• Scope that enclosed in { } is called a block.
45
Figure : Pass by value
Pass by Value
• You have been introduced with the term
“input parameter”. This type of parameter is
passed using “Pass by Value”.
47
Passing expression by value
Pass by Value - examples
Pass by Value - examples
#include <stdio.h>
#include <conio.h>
void printno(int a) ;
void printno(int a)
{
printf("\n Number is : %d", a);
}
void main()
{
int no;
void printno(int);
clrscr(); printf("\n Enter Number : ");
scanf("%d", &no); printno(no); getch();
}
Pass by Reference
• Passing by reference is a passing technique that
passes the address of a variable instead of its
value.
– That’s why it is also called Pass by Address
return;
}
Pass by Reference- examples
#include <stdio.h>
int main () {
return 0;
}
return;
}
Figure : Pass by reference
ARRAYS
Concept of an array
• An ordinary variable can only contain a single value.
• An array is a variable that contains a collection of values of
the same type. These values are stored in a sequential
location.
57
Concept of an array
59
Declaring and defining Arrays
• Since an array is a variable, it must be declared and
defined before it can be used.
• Declaration and definition tell the compiler:
– the name of the array
– the data type of each element
– the number of elements in the array
Syntax:
data_type variable_name[n]; // n = number of elements
60
Declaring and defining Arrays
61
Declaring and defining Arrays
Like ordinary variables, arrays may also be initialized:
62
Accessing elements in arrays
• We use an index to access an element from an array.
int A[2];
64
Assigning values into elements
Examples:
3. Assigning each element of array C with a value
that is twice its index
int C[9];
int i;
for (i=0; i<9; i++)
C[i] = i*2;
65
Assigning values into elements
Examples:
4. Assigning each element of array D with a value
that is read from the keyboard
int D[5];
int i;
66
Assigning values into elements
Examples:
5. The following example would be an error – elements of an
array must be assigned individually.
int E[4];
E = {10,20,30,40}; // this would be an error
67
Getting values from elements
Examples:
1. Assigning variable n with the value of first
element of array A.
int A[] = {1,3,5,7};
int n;
n = A[0];
68
Getting values from elements
Examples:
3. Assigning the first element of array C with the value of the
second element,
int C[] = {11,23,35,47};
C[0] = C[1];
69
Arrays and Functions
• Passing an element of an array to a function
can be in two forms:
- Pass by value - pass its content:
eg. printf(“%d”, A[2] );
- Pass by reference - pass its address.
eg. scanf(“%d”, &A[2] );
70
Arrays and Functions
Passing the whole array to a function can only be done by using pass by
reference.
- It is actually passes the address of the first element.
Example:
void main(void)
{
int A[3]={10,20,30};
increase(A); // or, increase(&A[0]);
} 71
Extra note/ summaries
Array declaration (pengisytiharaan tatasunan):
[ ] means array
20 means 20 element/ 20 box
int B [20]; B
array
char GRED [10];
72
Reference
[ 0 ] 1st element
int B[5]={26,3,6,107,20};
26 3 6 107 20
[0][1][2][3][4]
74
• C allows a character array to be represented by a character
string rather than a list of characters, with the null terminating
character automatically added to the end. For example, to store
the string "Merkkijono", we would write:
75
To read/print array using looping:
• Read:
for (i=0; i<5; i++)
{
scanf("%d ", &A[i]);
}
• Print:
for (i=0; i<5; i++)
{
printf("%d ", A[i]);
}
76
P L E
E X A M
77
#include <stdio.h>
printf("\n");
Array is use looping. If not:
increase(A); // or, increase(&A[0]); printf("%d ", A[0]);
printf("%d ", A[1]);
for (i=0; i<3; i++) printf("%d ", A[2]);
{
printf("%d ", A[i]);
}
} 78
#include <stdio.h>
void main()
{
char word[20]; output:
word[0] = 'H';
word[1] = 'e';
word[2] = 'l';
word[3] = 'l';
word[4] = 'o';
word[5] = 0;
printf("The contents of word[] is -->%s\n", word );
}
79
#include <stdio.h>
void main()
{
output:
char day1[7] = "Sunday";
char day2[7] = "Monday";
char day3[8] = "Tuesday";
char day4[10] = "Wednesday";
char day5[] = "Thursday";
char day6[] = "Friday";
char day7[] = "Saturday";