Example:
Write your own function that calculates arithmetic middle of three real numbers. Write additional main program that stores given three numbers and calls on your previous function, and then prints calculated arithmetic middle.
#include <stdio.h>
float arit_midd( float a, float b, float c ){
float ar;
ar = (a + b + c) / 3;
return ar; // How many values “return” may return?
}
int main(void) {
float x, y, z, midd;
printf("\nInput three real numbers : ");
scanf("%f %f %f", &x, &y, &z );
midd = arit_midd(x,y,z);
printf("\nArithmetic middle : %f", midd);
}
Example:
What will be printed after execution of a program that calls on a function?
void twotimes(int x) {
printf ("\nF:Argument before change took place %d",x);
x *= 2;
printf ("\nF:Argument after tempering with it %d",x);
}
int main(void) {
int number=10;
printf ("\nM:Number before function-call %d",number);
twotimes(number);
printf("\nM:Number after function-call is %d",number);
}
Result on-screen:
M:Number before function-call 10
F:Argument before change took place 10
F:Argument after tempering with it 20
M:Number after function-call 10
Change inside a function wasn’t saved after execution and return to main program! Why?
int twotimes(int x){
printf ("\nF:Argument before change took place %d",x);
x *= 2;
printf ("\nF:Argument after tempering with it %d",x);
return x;
}
int main(void) {
int number=10;
printf ("\nM:Number before function-call %d",number);
broj = twotimes(number);
printf("\nM:Number after function-call %d",number);
}
Result on-screen:
M:Number before function-call 10
F:Argument before change took place 10
F:Argument after tempering with it 20
M:Number after function-call 20
Example:
Compose a function that calculates value of a sinus func. as sum of n elements. Function’s argument is given in radians. Func. sinus is defined as:
#include <stdio.h>
#include <math.h>
//realization with use of no additional functions
float sinus(float x, int n){
int i, forsign;
float sum, element, fakt, xpot;
sum = 0.0;
xpot = x;
fakt = 1.0;
forsign = 1;
for( i=1; i<=n; i++ ) {
element = forsign * xpot / fakt;
forsign *= -1;
xpot *= x*x;
fakt *= (2*i) * (2*i+1);
sum += element;
}
return sum;
}
//realization with help of additional functions
long fakt( int n ){
int i;
long f=1;
for( i=1; i<=n; i++ ) {
f *= i; }
return f;
}
float sinus(float x, int n){
int i, forsign;
float sum, element; sum = 0.0; forsign = 1;
for( i=1; i<=n; i++ ) {
element = forsign * pow(x,2*i-1) / fakt(2*i-1);
forsign *= -1;
sum += element;
}
return sum;
}
Technorati Tags: C++, C, Program, Code, Visual Studio, Sinus, Function
This is good, and informative. Coincidentally I'm just browsing and this was something I had already learned, but I wanted to bring up a question: when you said to compose a function that calculates the value of a sinus function...did you mean sine? Sinus is the wrong word. I believe you meant sine, which you showed the use of above. I'm sorry to nitpick but I figured you should know.
I agree with AkutAq, though I admit in my language (Dutch) its also called "sinus" I think it would be better if you renamed it "sine", since that is the correct English term.
Thanks to the author. It's simple yet easy to understand.
great job !
Thanks, it's great to have lots of concrete examples