You are on page 1of 20

Algorithm and

Programming

Function and Recursive


Outline

▪ What is function?
▪ Why do we need function?
▪ Syntax of creating a function and using a function
▪ Function with return type
▪ Function with non-return type
▪ Examples of function
▪ Recursive function
▪ Syntax
▪ example

2
Function

3
Function
❑ What and Why?
▪ A function is a block of code that performs a specific task. A function may return a
value.

▪ Why function?
✓ A large and complex program can be divided into smaller programs
✓ assign task and work in team is easy

✓ Program is easy to understand, fix error, and maintain

✓ Reusable code

4
Function
❑ Type of function

▪ There are two types of functions in C programming:


❑ Standard library functions
Library Provided functions
▪ Also called: built-in function, or existing function stdio.h printf(), scanf()
▪ For example, see Table 1 math.h pow(), sqrt(), ceil(),
floor(), cos(), sin()
❑ User-defined functions
▪ Also called: user-custom function

▪ For example, a user (programmer) defines his/her own function to do something

5
Syntax
❑ Function with returning value
c) Parameter of a function
It has parameter type and parameter name
If have more than one, separate them by comma
type functionName(type parameter1){
... .. ... b) Name of function
... .. ... it should start with a verb

return value; int sumTwoNumber(int n1, int n2){


} int s;
s=n1+n2;

a) Type of value returning d) Return value return s;


from the function It must be the same data type as a) }

Ex1: Create a function to do


summation of two numbers 6
Syntax of Creating A Function
❑ Function with no returning value
c) Parameter of a function
It has parameter type and parameter name
If have more than one, separate them by comma
void functionName(type parameter1){
... .. ... b) Name of function
... .. ... it should start with a verb

return value;
}
void greetMessage(char name[20]){
printf(“Hi, %s”, name);
printf(“Welcome back!”);
a) Void means no returning d) No need to return value since we
value from the function use void in a) }

Ex1: Create a function to display a


greet message for a person 7
Syntax of Using a Function
❑ How to execute the created function?
▪ The execution of a C program begins from the main( ) function.
▪ Our user-defined function can be executed by calling it from main() function

#include <stdio.h> #include <stdio.h>


void functionName(){ int functionName1(){
... .. ... ... .. ...
return 5;
... .. ... }
} void functionName2(){
... .. ...
... .. ...
int main(){ }
... .. ...
... .. ... int main(){
int result;
... .. ...
functionName();
result=functionName1();
functionName2();
... .. ... ... .. ...
... .. ... ... .. ...
} } 8
Function
❑ Global Vs. Local variable
#include <stdio.h>
Global variables
▪ Local variable a variable that creates inside a int n=0;
function char name[10];
int sum(int a, int b){
✓ Its value can not be accessed from outside the int res=0;

Function
User-defined

Local variables
function
res=(a+b)*2;
✓ Its value will be destroyed after the function n++;
finish executing return res;
Output: }

int main(){
int num=10;
▪ Global value is variable that creates int res=2;

outside the function below all library inclusion printf("*Start program \n");
✓ Its value can be accessed from any functions printf(“%d”, sum(2,5));
res = sum(2,5)
✓ Its value will be destroyed after the whole printf(“%d”, res);
program finish executing printf(“%d”, n);
Call created function
} 9
Example of functions
❑ Returning Vs. Non-returning value function
▪ Function with returning value ▪ Function with no returning value

int sum(int a, int b){


int res=0; void display(int a){
printf(“Hello %d\n”, a);
res=(a+b)*2; printf(“This function has no returning value”);
n++; printf(“use the keyword void”);
return res; }
}

10
Q&A
11
Creating a simple function with return value

12
function with input from user

13
Function with return value and with no return value

14
Function in function

15
Using global variable in function

16
Practices
❑ Write a C++ program to solve the following problem.

1. Write a function to calculate this formula y=3x2-2x


where x is the parameter of the function. The function
returns the value of y. Test the function in main
program when x is set to 2, 5, 7 and 10.
What are the values of y? int eq1(int x)
17
Practice
❑ Write a C++ program to solve the following problem.

2. Write a function to display whether a person is


allowed to vote or not. This function have one
parameter which is the age of a person.
▪ A person is allowed to vote when his/her age is greater than
or equal 18.

Display the message either “You are allowed to vote” or


“You are not allowed to vote”
18
Practice
❑ Write a C++ program to solve the following problem.

3. Write a function that can find root of quadratic


equation ax2+bx+c=0. The function takes
coefficient a, b and c as the parameters.
void findRoot(a, b, c)

19
Practice
❑ Write a C++ program to solve the following problem.

4. Write a function to compute surface of a triangle using


heron formula. float computeSurface(x, y, b)

20

You might also like