C FUNCTIONS
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
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:
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 :
Example Explained :
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 chapter
Inside the function (the body), add code that defines what the function should do
C FUNCTIONS
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 ;
In the following example, myFunction() is used to print a text (the action), when it is called:
A function can be called multiple times:
OutPut :
C FUNCTIONS
C FUNCTIONS
For Example Calculate the Sum of Numbers :
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:
Output :
C FUNCTIONS
Parameters and Arguments
Information can be passed to functions as a parameter. Parameters act as variables inside the function.
Parameters are specified after the function name, inside the parentheses. You can add as many
parameters as you want, just separate them with a comma:
Syntax :
In the example below, the function takes a string of characters with name as parameter. When the function is
called, we pass along a name, which is used inside the function to print "Hello" and the name of each person:
Output :
C FUNCTIONS
Multiple Parameters
Inside the function, you can add as many parameters as you want:
Output :
If we consider the "Calculate the Sum of Numbers, we can make a more sustainable program
by using function parameters:
C FUNCTIONS
Output :
Pass Arrays as Function Parameters
You can also pass arrays to a function:
Output :
C FUNCTIONS
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:
Output :
This example returns the sum of a function with two parameters:
C FUNCTIONS
C FUNCTIONS
You can also store the result in a variable:
Output :
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:
Output :
C FUNCTIONS
Tip: If you have many "result variables", it is better to store the results in an array:
C FUNCTIONS
Real-Life Example :
To demonstrate a practical example of using functions, let's create a program that converts a
value from fahrenheit to celsius:
Output :
C FUNCTIONS
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
A variable created inside a function belongs to the local scope of that function, and can only be
used inside that function:
A local variable cannot be used outside the function it belongs to.
If you try to access it outside the function, an error occurs:
C FUNCTIONS
Output :
Global Scope
A variable created outside of a function, is called a global variable and belongs to
the global scope.
Global variables are available from within any scope, global and local:
Output :
C FUNCTIONS
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):
Output :
C FUNCTIONS
C Function Declaration and Definition
You have already learned from the previous topic that you can create and call a function in the
following way:
Output :
A function consist of two parts:
Declaration: the function's name, return type, and parameters (if any)
Definition: the body of the function (code to be executed)
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:
C FUNCTIONS
Output:
What About Parameters
It is considered good practice to write it like this instead:
C FUNCTIONS
Functions Calling Other Functions
As long as you declare functions first, it is also possible to use functions to call other functions:
C FUNCTIONS
C Recursion
Recursion is the technique of making a function call itself. This technique provides a way to
break complicated problems down into simple problems which are easier to solve.
Recursion may be a bit difficult to understand. The best way to figure out how it works is to
experiment with it.
Recursion Example :
Example Explained :
When the sum() function is called, it adds parameter k to the sum of all numbers smaller
than k and returns the result. When k becomes 0, the function just returns 0. When running,
the program follows these steps:
10 + sum(9)
10 + ( 9 + sum(8) )
10 + ( 9 + ( 8 + sum(7) ) )
...
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0
Since the function does not call itself when k is 0, the program stops there and returns the
result.
C FUNCTIONS
The developer should be very careful with recursion as it can be quite easy to slip into writing a
function which never terminates, or one that uses excess amounts of memory or processor
power. However, when written correctly, recursion can be a very efficient and mathematically-
elegant approach to programming.