You are on page 1of 11

Official (Closed) - Sensitive Normal

Chapter 4
Functions
By the end of this chapter, students will be able to:
Explain how a function works
Differentiate between local and global variables
Analyze programs that use functions
Write programs that use functions to solve problems

3/9/2021 - sun 38PROG - Chapter 4: Loops 1


Official (Closed) - Sensitive Normal

Topic 4.1
Introduction to Functions
By the end of this chapter, students will be able to:
Explain how a simple function works
Describe the structure of functions
Analyze programs that use simple functions
Write programs that use simple functions to solve problems

3/9/2021 - sun 38PROG - Chapter 4: Loops 2


Official (Closed) - Sensitive Normal
Introduction to Functions

• Consider the program on the


right: *****
*
• What does it do? ****
• It displays bigger size of EYE *
*****

• What are not so good about the * *


* *
program? *
• Difficult to read *
*
• Re-type the same codes
*****
*
• How can we improve it? ****
*
• Use functions *****

07/01/2023 38PROG - Chapter 5: Functions 3


Official (Closed) - Sensitive Normal
What is a Function?
• A function is a self-contained unit of program code designed to
accomplish a particular task

• Some of the functions we have used


• printf(), used to display messages on the monitor screen
• sqrt(), used to calculate square root of a number

• The examples above are the standard C library functions


• These functions are written by the compiler developers
• You don’t have to know how the functions work inside
• You only need to know how to use these functions

• User-defined Functions
• These are functions written by programmers, like yourself, so that the
code can be reused and the program will be more readable

07/01/2023 38PROG - Chapter 5: Functions 4


Official (Closed) - Sensitive Normal
Writing simple Functions
#include <stdio.h>

void BigE(void);
void BigY(void);

void main(void)
***** {
* BigE();
**** BigY();
* BigE();
}
*****
void BigE(void)
* * {
* * printf(" *****\n");
* printf(" *\n");
* printf(" ****\n");
* printf(" *\n");
printf(" *****\n\n");
}
*****
* void BigY(void)
**** {
* printf(" * *\n");
***** printf(" * *\n");
printf(" *\n");
printf(" *\n");
printf(" *\n\n");
}

07/01/2023 38PROG - Chapter 5: Functions 5


Official (Closed) - Sensitive Normal
Structure of Functions
#include <stdio.h>

• Function prototype void BigE(void);


void BigY(void);
• To tell the compiler the format of void main(void)
using the function {
BigE();
• Function call BigY()
BigE();
}
• To activate or use the function
• When the function is called, the void BigE(void)
{
program will go to its definition, printf(" *****\n");
printf(" *\n");
execute the codes and then return printf(" ****\n");
printf(" *\n");
back to continue printf(" *****\n\n");
}
• A function can be called many times
void BigY(void)
• Function definition {
printf(" * *\n");
• Contain the actual codes that perform printf(" * *\n");
printf(" *\n");
the task printf("
printf("
*\n");
*\n\n");
• Need to write only once }

• Need to be called (used) in order to


perform the task

07/01/2023 38PROG - Chapter 5: Functions 6


Official (Closed) - Sensitive Normal
How Functions Work
• In C, the first user function to run is
always main()
• Table below shows how the program
runs when we use functions
Line What does it do?
1 8 Call BigE()
2 13 Enter BigE()
3 15-19 Call printf()to display
4 9 Call BigY()
5 22 Enter BigY()
6 24-28 Call printf()to display
7 10 Call BigE()
8
13 Enter BigE()
9
15-19 Call printf()to display
10 11 End program

07/01/2023 38PROG - Chapter 5: Functions 7


Official (Closed) - Sensitive Normal
Time for Experiential Learning
Please refer to Brightspace for instruction

Do it

4.1a: How
What next? Functions Work What happened?

Why/how did it happen? Reference: https://youtu.be/oikYx9-zhxQ

07/01/2023 38PROG - Chapter 1: Fundamental of C 8


Official (Closed) - Sensitive Normal
Practical Exercise 4.1a: Simple Functions – MyInfo()
P4_1a.c: Complete the program and run it Sample output
Tony Stark
Diploma in AI & Robotics
School of Engineering
T.O.P. Polytechnic
Take note that your program must define, prototype, and
call the functions as required. Without meeting this
requirement, your program will be considered wrong even
if it produces the correct output.
Reflection
What mistake did you make?
What have you learnt?

Check point:

07/01/2023 38PROG - Chapter 1: Introduction to Programming 9


Official (Closed) - Sensitive Normal
Practical Exercise 4.1b: Simple Functions – Line()
P4_1b.c: Reflection
Rewrite previous program P4_1a.c to add a new function Line() What mistake did you make?
which prints 24 ‘=’s to form a line. What have you learnt?

Inside the MyInfo() function, call the Line() function as


appropriate to enhance the output as per the sample output below.

========================
Tony Stark
========================
Diploma in AI & Robotics
School of Engineering
T.O.P. Polytechnic
For reflection, you may consider the following:
========================
How many times the Line() function is called, and how many
times it is defined. What is your conclusion?

Check point:

07/01/2023 38PROG - Chapter 1: Introduction to Programming 10


Official (Closed) - Sensitive Normal
Practical Exercise 4.1c: Simple Functions – Square()
P4_1c.c: Complete and run the program Reflection
What mistake did you make?
What have you learnt?

Check point:

Enter an integer: 12
The square of 12 is 144.

07/01/2023 38PROG - Chapter 1: Introduction to Programming 11

You might also like