Parameters in C Programming - ProgrammerTech
Language:

Search

Parameters in C Programming

  • Share this:
Parameters in C Programming

Parameters in C Language

The parameter is considered a variable that is written and created when defining the function and it is part of the function, that is, when we create a new function, the parameter is the variable that is passed to the function in order to be dealt with and do the special task in the function.

In the previous lesson, we learned what functions are, and we said that functions are separate pieces of code from each other, and this means that all variables inside a particular function cannot be accessed in another function, and therefore to transfer values ​​from one function to another. Another function we pass values, but what is meant by passing?

 

Examples of how to handle parameters in C

Example 1

If we have a variable in the main function and we want to print the value of this variable inside the printMe function, for example.

#include <stdio.h>
#include <stdlib.h>
void printMe()
{
    printf("kaissar \n %d", age);
}
int main(int argc, char** argv) {
    int age = 28;
    printMe();
    return 0;
}

When we run the code, we notice that an error and a red alert appears at line 5 and the reason for this error is that the variable age has been defined in the main function. And as we said in the previous lessons that everything that is defined within a certain range, i.e. in curly brackets, is not. Defined outside of it will produce an error because the variable age is not defined in this range, and here there are more than one way we have but the best option is to pass the variable age inside the parentheses of the printMe function and everything written inside the parentheses is called the function parameter, we will write the parameter of the function between Parentheses at the beginning of the function.

void printMe(int age)

And when we run the code in this way, we will notice that the error that appeared previously on line 5 will disappear, but another error will be produced when calling the function in line 10 because the function expects a variable of type int to pass with it, so we will write the name of the variable age in the parentheses of the function and at Running the code will print the following

kaissar
28

It may be confusing for the first time but what happened is in the printMe function a special variable named age was defined in . Function header We said that the variables defined in parentheses are called parameters. And in the main function there is another variable called age and this variable has nothing to do with the variable above when void inside parentheses. We can call it whatever we want and that's the power of programming. We can name the same. Let's know that it is this variable that will be passed in this function and the value can be passed directly between the parentheses. When this function is called printMe(28) without having to write a full variable to store the value in.

When the function is called, the number 28 will be placed inside the function parameter inside the printMe function, which is the age parameter that distinguishes functions from others. For example, we can run the function printMe for more than one value, and each value does a different job. If we repeat the command to call the function inside main as follows

Example 2

#include <stdio.h>
#include <stdlib.h>
void printMe(int age)
{
    printf("kaissar %d\n", age);
}
    int main(int argc, char** argv) {
    printMe(30);
    printMe(28);
    printMe(25);
    return 0;
}

The kaissar name will be printed and followed by the number, meaning that the program has worked on more than one value and it is worth mentioning. The parameter age will be defined only when the function is called and in the first function call with a value of 30 done. Defining the age parameter in this call has a value of 30 until the function finishes its work and the function dies and then it is done. Calling the function again with a value of 28 with the same principle of work as the first and the same in the third call, that is, the value of age does not depend from one call to another.

kaissar 30
kaissar 28
kaissar 25

We can also pass more than one value instead of a single value such as age, i.e. an infinite number of values ​​can be passed.

Example 3

#include <stdio.h>
#include <stdlib.h>
void printMe(int age, double height)
{
    printf("Agge %d, Length %f\n", age, height);
}
int main(int argc, char** argv) {
    printMe(30, 1.9);
    printMe(28, 1.7);
    printMe(25, 1.3);
    return 0;
}

We notice that we have passed two values, the first is a scalar variable named age and the second is a double variable named height. From the program, we print the value of the first variable, then the second variable, and in the main function we pass three values ​​through the printMe function. We created it and when we run the code we will get the result.

Agge 30, Length 1.900000
Agge 28, Length 1.700000
Agge 25, Length 1.300000

Tags:
Saja Alsadig

Saja Alsadig

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