7-Functions In C Programming
C Language, C Programming, Code Blocks, Starting with C , Functions, Code in C
In C, modules are called functions. The C standard libraries contain many functions that perform general mathematical operations, character operations, input and output operations, and many other important operations.
Functions like printf, scanf, pow are standard library functions.
When using math library functions, we should put the math header file in our program by writing the #include<math.h> preprocessor command at the beginning of the program.
Standart Library Functions
- – – Function – – – – – – – Explanation
| sqrt(x) | square root of x |
| exp(x) | e^x exponential function |
| log(x) | logarithm of x to base e |
| log10(x) | logarithm of x to base 10 |
| fabs(x) | absolute value of x |
| ceil(x) | rounds x to the first integer greater than itself |
| floor(x) | rounds x to the first integer smaller than itself |
| fmod(x,y) | finds the remainder of the x/y operation |
| sin(x) | the sine of x (x radians) |
| cos(x) | cosine of x |
| tan(x) | the tangent of x |
/**** A program that squares integers from
1 to 10 and uses the square function ***/
//programmer-defined square function
#include<stdio.h>
int square(int); /*the first version (prototype) of the function*/
int main()
{
int x;
for(x=1; x<=10; x++)
{
printf("%d ", square(x));
printf("\n");
}
return 0;
}
/* definition of the function */
int square (int y)
{
return y*y;
}

The square function is called from the printf expression under main. The square function gets a copy of the x value thanks to the y parameter. Then the y*y calculation is made.
int square (int), “int” in parentheses indicated to the compiler that the square function expects to receive an integer from the calling function.
The leading int tells the compiler that the square function will return an integer result to the function that calls it.
Other Example Programs for Functions
User Defined Programs
In C, programs can be reviewed reversibly and irreversibly. Programs specified by “void” are usually used for printing, checking, and do not return a value. But for example, it is expected that some functions defined by “int”, “float”, etc. will return a value.
void message()
{
printf("Hello World!!");
}
void loop()
{
for(int i=0; i<=10; i++)
{
printf("%d", i);
}
}
void downTheLine ()
{
printf("\n");
}
int main()
{
message();
downTheLine();
loop();
}

Fibonacci Example with Function
the Fibonacci sequence, in which each number is the sum of the two preceding ones.
//fibonacci sequence example
#include<stdio.h>
long fibonacci(long);
int main()
{
long result, number; //the "long" type was chosen because the numbers
//increase rapidly in the fibonacci sequence
printf("Enter an integer: ");
scanf("%ld",&number); //ld was used to print "long" values
result=fibonacci(number);
//fibonacci value of entered numbere
printf("Fibonacci(%ld)= %ld\n", number,result);
return 0;
}
long fibonacci(long n)
{
if (n==0 || n==1)
return n;
else
return (fibonacci(n-1)+fibonacci(n-2));
}

RECURSION
Recursion is the process that occurs when a function calls a copy of itself to work on a smaller problem.
Recursion Example
//sum of natural numbers using recursion method
#include<stdio.h>
int addnumbers(int n);
int main()
{
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum= %d", addnumbers(num));
return 0;
}
int addnumbers(int n)
{
if(n!=0)
return n + addnumbers(n-1); //continues to add n and itself by subtracting 1
else
return n;
}

Storage Types of Variables
Local Variable
Variables defined in a block are local variables. They can be used in the block which they are defined.
int main()
{
int num1; //num1 is a local variable to main()
}
int func ()
{
int num2; //num2 is a local variable to func()
}
Global Variable
Variables defined outside of all functions are known as global variables and can be accessed from any function.
#include<stdio.h>
void printOnScreen();
int i=4; //global variable
int main()
{
....
....
}
Static Variable
The value of a static variable persists until the end of the program.
#include <stdio.h>
void printOnScreen();
int main()
{
printOnScreen();
printOnScreen();
}
void printOnScreen()
{
static int x = 1; //without static decleration
//x will defined as 1 again
x += 10;
printf("%d ",x); //without static result is 11 11
}

