function recap
TRANSCRIPT
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.
Common Type (FUNCTION)
Void function Without argument Void unction With Input argument Function with input argument and single
result
Void function without argument syntax: void functionname(void)
Example:display()draw_circle()draw_triangle()
The nearest explantion is printf()
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");
}
}
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)
Void function with input argument
syntax: functiontype functionname(input)
Example:double box(123.45)circumference(5.0)calculate(inum1)
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("***********");}
#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; }
VOID>> and what is the different between both function shown
void box(double num)
void draw_triangle(void)
Function with input argument and single result
syntax: functiontype
functinname(input argument)
Example:double circumferencecal(int rads);
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);
}
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);
Function with multiple arguments
Syntax:
functionname(argument1,argument2)
Example:circumferencecal(radian,PI);
#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);
}
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;
}
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.
GOTO
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
Example