You are on page 1of 7

Alreflections Digital Creative

https://alreflectionsdc.blogspot.com

A C function
In this tutorial, you will be introduced to functions (both user-
defined and standard library functions) in C programming. In
addition, you will learn why functions are used in programming.
First, a function is a block of code that performs a specific task.
With functions, complex programs are divided into smaller chunks
making the codes easy to understand and reuse.

Types of function
There are two types of function in C programming:
 Standard library functions
 User-defined functions
1. Standard library functions
The standard library functions are built-in functions in C
programming. For example, the printf() is a standard library
function to send formatted output to the screen. This function is
defined in the stdio.h header file.
2. User-defined function
You can also create functions just as you wish. Such functions
created by the user are known as user-defined functions. More
than that, you can even create header file yourself.
How user-defined function works?

The execution of a C program begins from the main() function.


And, the compiler starts executing the codes inside
functionName() when it’s called. As you can imagine,
functionName() in main refers to the codes inside the functions
called functionName.
The control of the program jumps back to the main() function once
code inside the function definition is executed. Keep in mind,
function names are identifiers and should be unique.

Function Prototype
A function prototype is a simple declaration of a function that
shows a function name, parameters and return type but with no
function body. The role of function prototyping is to inform the
compiler that the function may later be used in the program.
SYNTAX
return_type function_name(type1 argument1, type2 argument2);
EXAMPLE
int addup(int num1, int num2);
How return statement works

Function vs Variable
As you can see, what is called data type for variables is the return
type for functions. While variables end with no parenthesis, a
function should have parenthesis ( ) even when it takes no
arguments at all. When it comes to definition, you only need to use
an equal sign = to assign data to a variable. However, you have to
use the two curry brackets { } to define the behaviour of a function.
EXAMPLES
int num=20; //direct assignment at declaration
int add(int x, int y) { //start of function body
//codes at the function scope (level / environment)
return x+y ;
} //end of function body
Both functions and variables can be called; now, what happens!
First of functions process data, while variables store data. Once
you call a variable, it will give you the value stored in it but once
you call a function, it will perform and a task and can give out a
value through the return statement.

Function vs Label
However, don’t complicate Functions and Labels. In some cases,
they can be used interchangeably but they are very different.
While functions terminate, labels don’t and they are just named
and accessible fields in programmer codes. As C programs
execute line by line, calling a function will first execute all codes of
the function body before going the next. Once you call a label (we
use goto label_name), the program assumes the label’s first line is
the next line.

Recursive Function
A function that calls itself is called a recursive function.
Recursive functions can behave like loops, in some cases, they
even terminate.

Building Header Files


It’s easy, you just have to build your C function and save it in a
separate file of the (.h) extension.
Try saving these as maths.h
int add(int x, int y) {
return x+y ;
}
int subtract(int x, int y) {
return x-y ;
}
int multiply(int x, int y) {
return x*y ;
}
int divide(int x, int y) {
return x/y ;
}
Let’s use maths.h in a calculator program
#include <stdio.h>
#include "maths.h"
int main()
{
int num1;
printf("First Number:");
scanf("%i", &num1);
int num2;
printf("\nSecond Number:");
scanf("%i", &num2);
printf("\nWhat would you like to do?\n");
printf("1.Add 2.Subtract 3.Divide 4.Multiply\n");
int oper;
printf("Write the Number:");
scanf("%i", &oper);
switch(oper)
{
case 1:
printf("%i+%i=%i\n", num1, num2, add(num1, num2));
break;
case 2:
printf("%i-%i=%i\n", num1, num2, subtract(num1, num2));
break;
case 3:
printf("%i/%i=%i\n", num1, num2, divide(num1, num2));
break;
case 4:
printf("%i*%i=%i\n", num1, num2, multiply(num1, num2));
break;
default:
printf("Invalid operator * * *\n");
break;
}
main();
}
Now, save it with [any name].c

The best way to understand codes is by running them


© Mihigo ER Anaja
Write to me for more;
https://www.facebook.com/mihigoanaja
https://iblink.ning.com/members/mihigoeranaja

You might also like