- A function is a self-contained block of program statements that perform a particular task.
- It is a block of code which only runs when it is called. One can pass data, known as parameters, into a function.
- Functions are used in order to perform certain actions, and they are important for reusing the code: Define the code just once, and then use it many times.
- We also have predefined functions whose implementation we do not have to write, we can simply use these functions. For example, main() is a function, which is used to execute code, and printf() is a function; used to output/print text to the screen.
Create a Function
To create (often referred to as declare) your own function, specify the name of the function, followed by parentheses () and curly brackets {}.
void myFunction() {
// code to be executed
}
int main(){
//Calling the function
myFunction();
}
In the above code:
● myFunction() is the name of the function
● void means that the function does not have a return value. You
will learn more about return values later in the next section.
● Inside the function (the body), add code that defines what the
function should do
Call a Function
Declared functions are not executed immediately. They are “saved for
later use”, and will be executed when they are called.
To call a function, write the function’s name followed by two
parentheses () and a semicolon ; as shown in the example above.
C Function Parameters
Information can be passed into a function as a parameter.Parameters
act as variables inside the body of the function.
Parameters are clearly specified after the function name, inside
parentheses. One is allowed to add as many parameters as desired, given one condition : separate them with commas:
returnType functionName(parameter1, parameter 2, parameter 3){
//code to be executed
}
The function call must have the same number of arguments as there
are parameters, and the arguments must be passed in the same
order.
Let us see how to pass an array as a parameter with an example:
void myFunction(int myNumbers[5]) {
for (int i = 0; i < 5; i++) {
printf("%d\n", myNumbers[i]);
}
}
int main() {
//Declaring the array
int myNumbers[5] = {10, 20, 30, 40, 50};
//Passing the array as a parameter in the function call
myFunction(myNumbers);
return 0;
}
The function (myFunction) takes an array as its parameter (int
myNumbers[5]), and loops through the array elements with the for
loop. When the function is called inside main(), we pass along the
myNumbers array, which outputs the array elements.
Note that when you call the function, one only needs to use the name of the array when passing it as an argument to myFunction:myFunction (myNumbers).
However, the full declaration of the array is required in the function parameter (int myNumbers[5]).
Return Values
The void keyword, that is used in the previous examples, indicates that the
function should not return a value. If one wants the function to return a
value, one can use a data type (such as int,float,double, etc.) instead of
void, and use the return keyword inside the body of the function. For example if we want to write a function to add 2 float numbers:
float addition(float num1, float num2){
float result = num1 + num2;
return result;
}
int main(){
float ans;
//The value that the function will return will be stored in this variable
ans = addition(7.9, 8.6);
printf("%f",ans)
return 0;
}
Recursion in C
- A recursive function is one that calls itself directly or indirectly in order to solve a smaller version of its task until a final recursive call which does not require a self-call.
• An instance of a problem the solution of which requires no further
recursive calls is known as a base case.
Math Functions
There is also a list of math functions available that allows you to
perform mathematical tasks on numbers.
In order to use them, onemust include the math.h header file in the
program:
#include <math.h>
- Square Root: To find the square root of a number, use the sqrt() function.
- Round a number: The ceil() (Ceiling) function rounds a number upwards to its nearest integer,
and the floor() method rounds a number downwards to its nearest
integer, and returns the result. - The pow() (power) function returns the value of x to the power of value of y (x^y).
#include <stdio.h>
//The header file to include the math library
#include <math.h>
int main()
{
//To find the squareroot:
int num = 24;
printf("Square root of the number is: %f\n",sqrt(num));
//To round off the decimal using ceiling & floor functions
float n = 1.4;
printf("Ceiling: %f\n", ceil(n));
printf("Floor: %f\n", floor(n));
//To find x^y using power function.
int x = 4, y = 3;
printf("%f\n",pow(4,3));
return 0;
}
/* OUTPUT:
Square root of the number is: 4.898979
Ceiling: 2.000000
Floor: 1.000000
64.000000
*/
Other Math Functions
A list of other popular math functions (from the <math.h> library)
can be found in the table below:
Function: Description
abs(x): Returns the absolute value of x
acos(x): Returns the arccosine of x
asin(x): Returns the arcsine of x
atan(x): Returns the arctangent of x
cbrt(x): Returns the cube root of x
cos(x): Returns the cosine of x
exp(x): Returns the value of Ex
sin(x): Returns the sine of x (unit of x is in radians)
tan(x): Returns the tangent of an angle
For queries regarding the content, please drop a mail to loop@cumminscollege.in Links for in-depth studying and practice problems are sent via mail to students of the college on the date of publishing of the blog by Loop Club. The content is a part of DSA Bulletins started by Team Loop, to provide thorough knowledge and facilitate the learning of new topics of DSA. We are a group of students who are passionate about CP and DSA and want to create awareness and help students in the above topics.