function. outline intro. functions examples of functions prototypes of a functions local and global...
TRANSCRIPT
Function
OutlineIntro.
Functions
Examples of Functions
Prototypes of a Functions
Local and Global Variables
IntroductionOne large program under main() has the
disadvantages as errors are harder to find and debug (Large amount of codes).
Divide and Conquer is a solution to the aforementioned problem.
The original big program is divided into many small problems (Modules). The idea is to solve
the original problem by successively solving each small problem. The final solution is
combined and formed in the main() program.
Functions in C
4
BOSS(calling func)
WORKER(called func)
1. Give an assignment (call)
3. Report the results (return)
2. Work (computing)
main
worker1 worker3worker2
worker4 worker5
Functions in C
C Standard FunctionBasic functions in the C-Standard Library
provide fundamental elements for programming such as input/output (scanf/printf in stdio.h),
square rooting (sqrt in math.h) printf() #include
<stdio.h>
5
FUNCTIONS IN CFUNCTIONS IN C
C STANDARDLIBRARY
C STANDARDLIBRARY
PROGRAMMER DEFINED
FUNCTION
PROGRAMMER DEFINED
FUNCTION
Functions in CSimilarly we can write our own function to
perform specific task. This function is called Programmer-Defined Function.
Can be called as many times as desired.
6
Examples of functions
7
#include<stdio.h>
int main( ){
int i;
for(i = 1; i <= 10; i++) printf("%d ", i * i);
printf("\n”);return 0;
}
#include<stdio.h>
int square(int);
int main( ){
int i;
for(i = 1; i <= 10; i++) printf("%d ", square(i));
printf("\n”);return 0;
}
int square(int y){ return(y*y);}
8
x
square( )
y
COPY ARGUMENTS
COPY RETURN-VALUE
y * yprintf
#include<stdio.h>
int square(int);
int main( ){ int i;
for(i = 1; i <= 10; i++)printf("%d ", square(i));
printf("\n”); return 0;}
int square(int y){ return(y*y);}
Examples of functions
To declare a function
9
return-type function-name (parameterlist){
declarations;statements;
}
• return-type-value: the type of values returned by the function, e.g., int, float, char, void
• function-name: the name of the function• parameter-list: the list of variables passed by the caller to the function in
the exact same order• declarations: internal variables used within the function
• statements: set of commands within the function
Different examples of functions#1
void func_name (){ statements
…..}
#2void func_name (param-
list){ statements
…..}
#3 Non-void func_name()
{ statements…..
}#4 Non-void func_name(param_list)
{ statements…..
}
* non-void refers to a non-void type such as int, float, char, etc.
11
void main(){ my_print();} void my_print(){ printf(“Hello world”);}
main my_print
1. call 2. Print
3. return
Example 2: param_list, return (void)
12
void main(){ my_print(2);} void my_print (int x){ printf(“%d”, x);}
main my_print
1. Call(2) 2. Print
3. Return
Example 3: no param_list, non-void return
13
void main(){ char ch; ch = my_print(); printf(“%c\n”, ch);} char my_print(void){ char lch; printf(“Enter your character: ”); scanf(“%c”, &lch); printf(“\n”); return lch;}
main my_print
1. Work 2. Print
3. Return
lch
Example 4: param_list, non-void return
14
void main(){ char ch; ch = my_print(5); printf(“%c\n”, ch);} char my_print(int x){ char lch; printf(“Enter your character: ”); scanf(“%c”, &lch); while (x > 0) {
printf(“%c”, lch);x--;
} printf(“\n”); return lch;}
main my_print
1. Work(5) 2. Print
3. Return
lch
Prototype of function
15
#include<stdio.h>
int square(int);
void main( ) { int x=3;
printf(“Square of %d = %d\n”, x, square(x));}
int square(int y) { return y * y;}
Prototype is usually listed ahead of main(). It is a way to let other programs aware of the param_list and return-type
of the function. It is extremely useful during the code-checking step when the project is being built (compiled).
int square(int); //This is an example of function prototype
PROTOTYPE
CALLING
DEFINITION
Che
ckin
g se
quen
ce
Without prototype, this would have been an error
Function Prototype
16
#include <stdio.h> int maximum(int, int, int); /*function prototype */
int main(){ int a, b, c;
printf(“Enter three integers: “);scanf(“%d%d%d”, &a, &b, &c);printf(“Maximum is: %d\n”,
maximum(a, b, c);
return 0;} /* Function maximum definition */int maximum(int x, int y, int z){
int max = x;if(y > max)
max = y;if(z > max)
max = z;
return max;}
int maximum(int, int, int);
Function Prototype
17
#include <stdio.h> int maximum(int x, int y, int z){
int max = x;if(y > max)
max = y;if(z > max)
max = z;
return max;}
int main(){ int a, b, c;
printf(“Enter three integers: “);
scanf(“%d%d%d”, &a, &b, &c);
printf(“Maximum is: %d\n”, maximum(a, b, c);
return 0;}
In this case, no prototype is needed in the main program. Since, maximum function is
defined (being checked) before main(). However, this order is
unpopular. Defining main() first is the prefered style.
Local and Global Variables
18
• Local Variables are created inside a function. They are only recognized internally. Other functions usually do not
know them. Local variables are destroyed immediately at the end of calling the function.
• Global Variables are created outside functions. They can be recognized by every functions. Only exception is in a
function who declares local variables of the exact same name.
Local and Global Variables
19
#include<stdio.h>
int ans = 0; int inc_one(int); /* function prototype */ void main(){
ans = inc_one(3);printf(“Answer is %d\n”, ans);
}
/* function definition: return x+1 */int inc_one(int x){
int ans, b;
b = 2;ans = x + b;return ans;
}
ans is a global variable declared outside the main
function. Note that main can use it right away.
x, ans, b are local variables inside inc_one function. ans in this function is different from the global ans declared earlier.
Local and Global Variables
20
#include<stdio.h>
void my_func(); //prototype of my_func
void main(){
int x=3;
printf(“Main: Before call function x=%d\n”, x);
my_func(); //call my_funcprintf(“Main: After call function x=%d\
n”, x);}
void my_func(){
int x;
x=2;printf(“My_func: x=%d\n”, x);
}
(local) x inside the main function is different from the (local) x
inside the my_func. x is 3 inside main(), but x is 2 inside my_func.
Main: Before call function x=3My_func: x=2Main: After call function x=3
Local and Global Variables
21
#include<stdio.h>
void my_func(); //prototype of my_funcint x;
void main(){
printf(“Main: Before call function x=%d\n”, x);
my_func(); //call my_funcprintf(“Main: After call function x=%d\
n”, x);}
void my_func(){
x=2;printf(“My_func: x=%d\n”, x);
}
x in this example is a global variable. Changing x inside
my_func also affects the value of x used in main.
Main: Before call function x=3My_func: x=2Main: After call function x=2