Function recap

20

Click here to load reader

Transcript of Function recap

Page 1: Function recap

FUNCTION RECAP

Page 2: Function recap

WHAT IS FUNCTION?

A function is a section of a program that performs a specific task .

Solving a problem using different functions makes programming much simpler with fewer defects .

It’s a solution for a big project that split into small sub project.

Page 3: Function recap

Common Type (FUNCTION)

Void function Without argument Void unction With Input argument Function with input argument and single

result

Page 4: Function recap

Void function without argument syntax: void functionname(void)

Example:display()draw_circle()draw_triangle()

The nearest explantion is printf()

Page 5: Function recap

Void function without argument

void draw_triangle(void){

int row,space,star;

for(row=1;row<=9;row++)

{

for(space=8;space>=row;space--)

{

printf("%c",288);

};

for (star=1;star<row;star++)

{

printf("* "); }

printf("\n");

}

}

Page 6: Function recap

Why void? (most common seen in function without argument)

Void is use when the function does not return any value

More explanation:

The subfunction is called draw_triangle()from the main function but the function

prototype is written as void draw_triangle(void)

Page 7: Function recap

Void function with input argument

syntax: functiontype functionname(input)

Example:double box(123.45)circumference(5.0)calculate(inum1)

Page 8: Function recap

Void function with input argument

void box(double num){

printf("***********\n");printf("* *\n");printf("* *\n");printf("* %7.2f *\n",num);printf("* *\n");printf("* *\n");printf("***********");}

Page 9: Function recap

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

void box(double num){printf("***********\n");printf("* *\n");printf("* *\n");printf("* %7.2f *\n",num);printf("* *\n");printf("* *\n");printf("***********");}

int main(){ box(123.45);getch();return 0; }

Page 10: Function recap

VOID>> and what is the different between both function shown

void box(double num)

void draw_triangle(void)

Page 11: Function recap

Function with input argument and single result

syntax: functiontype

functinname(input argument)

Example:double circumferencecal(int rads);

Page 12: Function recap

Function with input argument and single result#include<stdio.h>

#include<conio.h>

double circumferencecal(int rads);

int main(){

int rads=5; double circum;

circum=circumferencecal(rads);

printf("Ukur lilit bagi bulatan bagi ber-radius-kan %d adalah %.4lf",rads,circum);

getch();

return 0;

}

double circumferencecal(int rads){

double x;

float pi=3.1415;

x=2*pi*rads;

return(x);

}

Page 13: Function recap

Returning values• The result of the function can be given back to the calling

functions

• Return statement is used to return a value to the calling function

• Syntax:

return (expression) ;

• Example:

return(iNumber * iNumber);

return 0;

return (3);

return;

return (10 * i);

Page 14: Function recap

Function with multiple arguments

Syntax:

functionname(argument1,argument2)

Example:circumferencecal(radian,PI);

Page 15: Function recap

#include<stdio.h>

#include<conio.h>

double circumferencecal(int rads,double PI);

int main(){

int rads=5; double circum,PI=3.1415;

circum=circumferencecal(rads,PI);

printf("Ukur lilit bagi bulatan bagi ber-radius-kan %d adalah %.4lf",rads,circum);

getch();

return 0;

}

double circumferencecal(int rads,double PI){

double x;

x=2*PI*rads;

return(x);

}

Page 16: Function recap

Chotto complicated example#include<stdio.h>

#include<conio.h>

#include<math.h>

double circumferencecal(int rads,double PI){

double x;

x=2*PI*rads;

return(x);

}

double areacal(int rads,double PI){

double y;

y=PI*pow(rads,2);

return(y);

}

int main(){

int rads=5; double area,circum,PI=3.1415;

circum=circumferencecal(rads,PI);

area=areacal(rads,PI);

printf("Ukur lilit bagi bulatan bagi ber-radius-kan %d adalah: %.4lf\nManakala luasnya pula: %.4lf",rads,circum,area);

getch();

return 0;

}

Page 17: Function recap

Do and Don’t in Function

DODo use local variable whenever possible

Do limit each function to a single task

DON’TDon’t try return a value that has a type different from the function’s type

Don’t let functions get too long. If a function starts getting long, try to break it into separate smaller tasks.

Don’t have multiple return statements if they aren’t needed. You should try to have one returns when possible; however, sometimes having multiple return statement is easier and clearer.

Page 18: Function recap

GOTO

Page 19: Function recap

GO TO

Is a function in programming c that allows you to jump according to designation labels

Syntax:goto label;Just simply means to redirect you to the label located in the coding

label: Just simply means to redirect you to the label

Page 20: Function recap

Example