Functions
Functions
Functions are used to perform certain actions, and they are important for
reusing code: Define the code once, and use it many times.
Predefined Functions
So it turns out you already know what a function is. You have been using it the
whole time while studying this tutorial!
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:
Example
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
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 {}:
Syntax
Void myFunction() {
// code to be executed
}
Example Explained
void means that the function does not have a return value. You will learn
more about return values later in the next chapter
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.
Example
#include <stdio.h>
// Create a function
Void myFunction() {
Int main() {
return 0;
}
You can put almost whatever you want inside a function. The purpose of the
function is to save the code, and execute it when you need it.
Like in the example below, we have created a function to calculate the sum
of two numbers. Whenever you are ready to execute the function (and
perform the calculation), you just call it:
#include <stdio.h>
// Create a function
Void calculateSum() {
Int x = 5;
Int y = 10;
Int sum = x + y;
Int main() {
return 0;
Syntax
// code to be executed
#include <stdio.h>
Int main() {
myFunction(“Liam”);
myFunction(“Jenny”);
myFunction(“Anja”);
return 0;
Multiple Parameters
Inside the function, you can add as many parameters as you want:
#include <stdio.h>
Void myFunction(char name[], int age) {
Int main() {
myFunction(“Liam”, 3);
myFunction(“Jenny”, 14);
myFunction(“Anja”, 30);
return 0;
Return Values
The void keyword, used in the previous examples, indicates that the function
should not return a value. If you want the function to return a value, you can
use a data type (such as int or float, etc.) instead of void, and use the return
keyword inside the function:
#include <stdio.h>
Int myFunction(int x) {
Return 5 + x;
Int main() {
Return 0;
}
If we consider the “Calculate the Sum of Numbers” example one more time,
we can use return instead and store the results in different variables. This
will make the program even more flexible and easier to control:
Example
#include <stdio.h>
Return x + y;
Int main() {
Return 0;
C VARIABLE SCOPE
Now that you understand how functions work, it is important to learn how
variables act inside and outside of functions.
In C, variables are only accessible inside the region they are created. This is
called scope.
Local Scope
#include <stdio.h>
void myFunction() {
int x = 5;
printf("%d", x);
int main() {
myFunction();
return 0;
#include <stdio.h>
Void myFunction() {
Int x = 5;
}
Int main() {
myFunction();
Printf(“%d”, x);
Return 0;
Global Scope
Global variables are available from within any scope, global and local:
#include <stdio.h>
// Global variable x
Int x = 5;
Void myFunction() {
Printf(“%d\n”, x);
Int main() {
myFunction();
Printf(“%d\n”, x);
Return 0;
Naming Variables
If you operate with the same variable name inside and outside of a function,
C will treat them as two separate variables; One available in the global scope
(outside the function) and one available in the local scope (inside the
function):
Example
The function will print the local x, and then the code will print the global x:
#include <stdio.h>
// Global variable x
Int x = 5;
Void myFunction() {
Int x = 22;
Int main() {
myFunction();
printf(“%d\n”, x); // Refers to the global variable x
return 0;
However, you should avoid using the same variable name for both globally
and locally variables as it can lead to errors and confusion.
In general, you should be careful with global variables, since they can be
accessed and modified from any function:
#include <stdio.h>
// Global variable
Int x = 5;
Void myFunction() {
Int main() {
myFunction();
return 0;
Conclusion
To sum up, use local variables (with good variable names) as much as you
can. This will make your code easier to maintain and better to understand.
However, you may find global variables when working on existing C
programs or while collaborating with others. Therefore, it is good to
understand how the scope works and how to use it effectively to make sure
your code is clear and functional.
Example
#include <stdio.h>
// Create a function
Void myFunction() {
Int main() {
return 0;
Declaration: the function’s name, return type, and parameters (if any)
}
For code optimization, it is recommended to separate the declaration and the
definition of the function.
You will often see C programs that have function declaration above main(),
and function definition below main().
This will make the code better organized and easier to read:
Example
#include <stdio.h>
// Function declaration
Void myFunction();
Int main() {
return 0;
// Function definition
Void myFunction() {
}
What About Parameters
#include <stdio.h>
Return x + y;
Int main() {
Return 0;
#include <stdio.h>
void myFunction();
void myOtherFunction();
int main() {
return 0;
// Define myFunction
void myFunction() {
// Define myOtherFunction
void myOtherFunction() {
Recursion
Recursion may be a bit difficult to understand. The best way to figure out
how it works is to experiment with it.
Recursion Example
Adding two numbers together is easy to do, but adding a range of numbers is
more complicated. In the following example, recursion is used to add a range
of numbers together by breaking it down into the simple task of adding two
numbers:
#include <stdio.h>
Int main() {
Printf(“%d”, result);
Return 0;
Int sum(int k) {
If (k > 0) {
} else {
Return 0;
}Math Functions
There is also a list of math functions available, that allows you to perform
mathematical tasks on numbers.
To use them, you must include the math.h header file in your program:
#include <math.h>
Square Root
Example
Printf(“%f”, sqrt(16));
Round a Number
The ceil() 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:
Example
Printf(“%f”, ceil(1.4));
Printf(“%f”, floor(1.4));
Power
Example
Power
The pow() function returns the value of x to the power of y (xy):
Example
printf("%f", pow(4, 3));