Reflection: Funtion
I, Summary
There are four components to a C function:
- its return value
- name
- parameters
- and associated code block( function body).
Functions need to be defined before they are used. A function declaration tells the compiler what the function’s inputs and outputs look like. By providing the data types for the return value and parameters of a function, the compiler can make sure that you’re using it properly without knowing what it actually does .The corresponding implementation attaches a code block to the declared function.
II, Applied to solve problems
1. Implement functions with the following prototypes:
int power(int m, int n) – In: m, n; Out: mn
I wrote a program. It just ran with small limited, because data type of output is int. It's too small with big data. So I try to instead data types.
int gt(int n) – In: n; Out: n!
Although, output is int, I replace it by double to have more range.
double expx(double x, double epsi) – In: x, epsi; Out: ex with precision < epsi
Calculate e^x, this problem have been solved in last post, but in this post, I will remove it to a function.
double sinx(double x, double epsi) – In: x, epsi; Out: sin(x) with precision < epsi
We use this formula:
We can realize that we can call some functions we wrote above: gt(x), Pow(n, m). It;s to easy to write this function.
double pi(double epsi) – In: epsi; Out: PI number with precision < epsi
int highest(double epsi) – In: epsi; Out: Highest n so that 1/n >= epsi
int highest1(double epsi) – In: epsi; Out: Highest n so that 1/n! >= epsi
int lowest(int m) – In: m; Out: Lowest n so that m < 2n (ex: m=10 => n=4)
2. Propose yourself 2 function and implement them.
I wrote 2 functions by myself. A function is to calculate sum all of numbers from 1 to n, the others is to calculate Greatest Common Divisor of 2 numbers.
Then we call them on main function to implement them:
III, Some of my thoughts
Using function help me a lot. I don't waste time to type some code again, then it's so fast. A function can make my program become more convenient and easy-to-look. There are some tips you should remember:
If function has no return value (returnType is void) then exclude any expression from return statement, alternatively, we can omit return statement: ex: return;
Terminate function and return value for function: ex: return(0); return(x); return(x*x + y); …
Sometimes, I have some mistakes with this problem. So if I remember, I can solve it.
There are four components to a C function:
- its return value
- name
- parameters
- and associated code block( function body).
Functions need to be defined before they are used. A function declaration tells the compiler what the function’s inputs and outputs look like. By providing the data types for the return value and parameters of a function, the compiler can make sure that you’re using it properly without knowing what it actually does .The corresponding implementation attaches a code block to the declared function.
![]() |
Function declarations vs. implementations |
II, Applied to solve problems
1. Implement functions with the following prototypes:
int power(int m, int n) – In: m, n; Out: mn
Although, output is int, I replace it by double to have more range.
double expx(double x, double epsi) – In: x, epsi; Out: ex with precision < epsi
Calculate e^x, this problem have been solved in last post, but in this post, I will remove it to a function.
double sinx(double x, double epsi) – In: x, epsi; Out: sin(x) with precision < epsi
We use this formula:
We can realize that we can call some functions we wrote above: gt(x), Pow(n, m). It;s to easy to write this function.
int highest(double epsi) – In: epsi; Out: Highest n so that 1/n >= epsi
int highest1(double epsi) – In: epsi; Out: Highest n so that 1/n! >= epsi
int lowest(int m) – In: m; Out: Lowest n so that m < 2n (ex: m=10 => n=4)
2. Propose yourself 2 function and implement them.
I wrote 2 functions by myself. A function is to calculate sum all of numbers from 1 to n, the others is to calculate Greatest Common Divisor of 2 numbers.
Then we call them on main function to implement them:
III, Some of my thoughts
Using function help me a lot. I don't waste time to type some code again, then it's so fast. A function can make my program become more convenient and easy-to-look. There are some tips you should remember:
If function has no return value (returnType is void) then exclude any expression from return statement, alternatively, we can omit return statement: ex: return;
Terminate function and return value for function: ex: return(0); return(x); return(x*x + y); …
Sometimes, I have some mistakes with this problem. So if I remember, I can solve it.
Comments
Post a Comment