Lesson 16: Functions in C


E-mail this post



Remember me (?)



All personal information that you provide here will be governed by the Privacy Policy of Blogger.com. More...



Functions are essential part of code in C and C++ language, so be sure to carefully read this lesson. You’ll notice there’s nothing to be afraid of – they are really easy to understand, and sometimes, can lighten up our program code significantly. In a way, they remind us of our main program. Functions usually return value that we use in our main block, but sometimes they return nothing. Either way, they do their task: like printing on screen or calculating equations. C++ Maniac presents you another interesting tutorial!


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:
, , , , , ,



6 Responses to “Lesson 16: Functions in C”

  1. Anonymous AkutAq 

    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.

  2. Anonymous Mortis 

    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.

  3. Anonymous zafar 

    good job

  4. Anonymous annwhyjay 

    Thanks to the author. It's simple yet easy to understand.

  5. Anonymous Prateej 

    great job !

  6. Anonymous leo 

    Thanks, it's great to have lots of concrete examples

Leave a Reply

      Convert to boldConvert to italicConvert to link

 


German Flag Spanish Flag French Flag Italian Flag Portuguese Flag Japanese Flag Korean Flag Chinese Flag British Flag


This Website is optimized for Firefox. Users browsing with Internet Explorer may encounter problems while viewing pages.


C++ Maniac



Learn C



Additional



#include



Learn Converting



Appendix


Links


Previous posts




Daily Lessons for programming in Visual Studio, using C code.