Functions in C Programming - ProgrammerTech
Language:

Search

Functions in C Programming

  • Share this:
Functions in C Programming

Functions in C Language

Functions The term function may be familiar in mathematics, but in programming it has deeper meanings than mathematics. How to create it and how to deal with texts in C.

Most of the programs we see are much larger than what we used to do in previous lessons and the best way to create and maintain large programs. It is dividing the program into more than one piece of software, as each piece performs special tasks, and thus the maintenance of each piece separately is much easier than if a problem occurred in a large program with one piece.

 

Benefits of functions in C language

There are many things that benefit us and that we should use functions with, namely:

  1. Calling functions built into functions, the programmer does not have to write the functions himself.
  2. Facilitates code reuse (so that the same function can be called in order to perform a task in different places in the program or outside the program).
  3. Functions make programming easier and more enjoyable by deduplicating code.

Example 1

#include <stdio.h>
#include <stdlib.h>
void printMuhammad(){
    printf("Muhammad Alush \n python \n javascript \n java \n c++ \n \n");
}
void printSaja(){
    printf("Saja Osman \n C \n PHP \n \n");
}
void printKaissar(){
    printf("Ahmed Kaissar \n html \n css \n laravel \n \n");
}
int main(int argc, char** argv) {
    printMuhammad();
    printSaja();
    printKaissar();
    return 0;
}

We note that we have defined three void functions, and these functions are:

The first function is called printMuhammad. This function prints the name of Muhammad and the languages ​​he provides on this site. The second function is printSaja and this prints the name of Saja and the languages ​​you provide on our website. The third function is printKaissar This function prints the languages ​​that Ahmed provides on our site.

Then we entered the main function and we called each function with its name as we wrote above, with parentheses at the end of each function to identify it, and when we run the previous code, we will get the following result.

Muhammad Alush
  python
  javascript
  java
  c++
Saja Osman
  C
  PHP
Ahmed Kaissar
  html
  css
  laravel

The bottom line of this example is that instead of writing all the commands in the main function, we distribute them into separate functions, the data for each name in a function alone and then call them only through the main function.

Example 2

Write a program containing a function called printLink that prints a programmer tech link.

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
    printLink();
    return 0;
}
void printLink()
{
    printf("www.programmer-tech.com \n");
}

We've built a printLink function and written to it that its job is to print our sitelink and then go down a newline, this was outside the main function. Then inside the main function we write a command to call this function by typing its name, and when we run this code it will give us an error. It does not print the result we want and the error is that the main function did not recognize a call. The printLink function has its own name and is undefined. The reason is that in C programs, the code executes line by line from top to bottom, starting with include, then int main, and other things to the end of the program.

But in our previous code, the compiler will come to the callback command printLink and the editor says I didn't see this function. at the top and he won't be able to recognize it because it's at the bottom, and the solution is to move it up over the int main.

#include <stdio.h>
#include <stdlib.h>
void printLink()
{
    printf("www.programmer-tech.com \n");
}
int main(int argc, char** argv) {
    printLink();
    return 0;
}

And when you move it and run it, the program will pass the function definition at the top and then it will pass the function. main and siri. function and says I have seen it at the top and when I run the result will be.

www.programmer-tech.com

note

As we said that the main function is the starting point of any program, let us represent it with the manager and there are some functions defined in the program such as myLink. Let us represent them by the employees. If the main function calls the employee to perform a task, the main function waits for the employee to finish his work and does not care about how he performs his work. He finishes the job

 

Functions that return the value of

Returned values ​​from functions ( Return type functions ) and to understand this type more we must take a live example of this, so imagine with me the following:

That a father calls the son to go to the market and the father is the main and the son is another function that goes to the market. If the father calls his son and says, “Go to the market,” he did not give him any data or transactions, and he did not ask him to return anything, and this is the first type of function “where there are no data and no references are required.” If the father said to the son, “Go to the market and take a dinar.” He passed a variable, which is the dinar, but he still did not ask the son to return anything, and this second type of function “where there are data, which is money and does not require any returns.”

But if he says, “Go to the market and take a dinar and bring me potatoes,” then the father asks the son to take the dinar and go to the market and then return to the father with the potatoes, meaning that he returned his value and this is the third type of function. And the father may say, “Go to the market and bring me potatoes, and he did not give him anything.” This means that the son should act to take money from anywhere else and go to the market, and this is the fourth type of function.

 

Function types that return a value in C

  1. It is without data and without references.
  2. With data and without references.
  3. With data and with references.
  4. Without data and with references.

 

Functions that return a value (without arguments and without references)

#include <stdio.h>
#include <stdlib.h>
void potato()
{
    printf("potato \n");
}
int main(int argc, char** argv) {
    potato();
    return 0;
}

We have created a void function called potato and this function was to print the word potato only. Then it goes down a new line and then we enter the main function and we call this function by just writing its name potato(). And when we run the previous code, we will get the result.

potato

We note that this type did not give him any data and we did not ask him to return anything.

 

Functions that return a value (with arguments and without references)

#include <stdio.h>
#include <stdlib.h>
void potato()
{
    printf("potato \n");
}
void printNumberPotato(int number)
{
    for(int i = 0; i < number; i++)
        printf("potato \n");
}
int main(int argc, char** argv) {
    potato();
    printNumberPotato(4);
    return 0;
}

We created a void function that requests int numeric data and the name of this function is printNumberPotato. We entered a for loop and said i is 0 and i is less than the variable number and it increases by 1 in each cycle and in each cycle it prints the word potato and drops a new line. Then we entered the main function to run it by calling it with its name printNumberPotato and we passed it the value 4 because at the top we set that it is necessary to pass a value.

When you run this program, it will print the word “potato” for us five times, and when you run it, we will find its result.

potato
potato
potato
potato
potato

This is the second type as it is of type void with a value entered.

 

Functions that return a value (with arguments and with references)

#include <stdio.h>
#include <stdlib.h>
int power(int number)
{
    return number * number;
}
int main(int argc, char** argv) {
    int result = power(4);
    printf("%d", result);
    return 0;
}

We created a function called power, which means squared, in order to return a square value, and we asked that the value passed is a numeric number, and this function prints the return of the value of the passed value multiplied by itself number*number, that is, returns the square value of this parameter. In the main function, we set a scalar variable result that takes the return value from the power function and multiplies it by the same value. Then we asked to print the return value, for example, we passed it the value 6, you will multiply it by 6 and the result will be 36, but in our program we passed it the value 4.

And when running, we get the result

16

We also note that the number 16 was printed for us because the value was 4 and multiplied by itself

 

Functions that return a value (without parameters and with references)

#include <stdio.h>
#include <stdlib.h>
int power(int number)
{
    return number * number;
}
void power2(int number)
{
    printf("%d", number * number);
}
int main(int argc, char** argv) {
    power2(6);
    return 0;
}

We copied the power function in the previous example and called it power2 and it prints an integer value which is number*number. In the main program, we called her by her name and passed the number 6 to her. And when you run the program will be the result.

9

The returned function must receive its value from the function that called it, the main function, and must be defined according to the type of value to be returned. We can define functions with any type of data that we have learned previously, such as (int, float, doubel, boolean) and others. But you should never forget to put return at the end of the definition of every function that returns a value.


Saja Alsadig

Saja Alsadig

من السودان مبرمجة مواقع ويب و تطبيقات أندرويد اتقن لغات "html, css, php, js" بالإضافة للتعامل مع اندرويد ستوديو و بناء التطبيقات, مهتمة بمجال التقنية منذ الصغر و كان لدي شقف كبير للدخول بالجامعه بقسم البرمجة و دخلته في عام 2013 و تعلمت كل شيئ ضمن الجامعة و خارجها من التعليم الذاتي و اتمنى أن أكون عند حسن ظن الجميع و إيصال الأمانة لكم, اعمل حاليا في إدارة موقع جامعة السودان.