You are on page 1of 2

Lesson 16: Functions in C - C Tutorial 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 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.

they

#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",num ber); broj = twotimes(number); printf("\nM:Number after function-call %d",nu mber);}

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 funct ions float sinus(float x, int n){ int i, forsign; float sum, element, fakt, xp ot; 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 functionslong 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, f orsign; 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

You might also like