Friday, July 6, 2018

C programming functions



C functions is next concept in C online tutorials. Here, you will also learn about defining functions in C. Additionally there is function declaration concept. Moreover, you will know how to call a function in C. Finally you will have idea about function arguments. However, it is better that you practise C function example exercise.

What is C function?

Function in C is group of statements. These statements will perform tasks together. In addition, C programs have minimum one function. This is main(). However, programs may define other functions as well. Moreover, dividing code is easy. Because you divide code in separate functions. You must remember that function will perform only specific task.


How to define function in C?

To define function in C check following form. Moreover, you must know that C program function has different functions. This will have function header and function body.

Different parts of function

Return type

Functions may return values. Therefore, return_type is data type of value. As this returns functions. In addition, functions perform operations. Even if they do not return value. Thus, return_type is keyword void.

Function name

Function name is name of function. Furthermore, there is function signature. And it is function name with parameter list.

Parameters

You will pass value to parameter when function invokes. Here, value is actual parameter. Some people refer it as argument. Besides, parameter list is about type, order or even number of parameters of function. But some functions may not have parameters.

Function body

It has collection of statements. These will define work of function.
General form of function definition in C language
return_type function_name( parameter list ) {
body of the function
}
Example program

Define function declaration in C

Function declaration in C programming language informs compiler. Declaration tells function name. Moreover, it will tell compiler on how to call function. You may even define actual body of function separately. When you define function in one source file then you use function declaration. But remember that calling of function is in another file. Therefore, you must declare function. However, it is at top of file calling function.

How to call function in C program

Remember that when creating C function, you must define task of function. In addition, for using function, you must call it. As a result when program calls function. Program control transfers to called function.
Thus call function will perform task. You can store returned value. This will happen only if function returns value.
Program example

What is meaning of function arguments?

When function uses arguments in C programs, you must follow rules. First rule is to declare variables. These variables must accept argument’s values. Hence these variables are formal parameters of functions. Behaviour of formal parameters is similar to local variables. They create when they enter function. However, they destroy when exiting function.
There are two ways to call functions. Because it is through main function arguments in C. Keep reading to know about function arguments in C language.
Call type in functions argumentsDescription
Call by valueCall by value method will copy actual value of argument in function’s formal parameter. In addition, parameter changes will not affect function
Call by referenceCall by reference method shall copy address of function argument in C program in formal parameter. In function, you use address to access actual argument in call. Thus, parameter changes will affect argument
The default function arguments in C is call by value. This way passing function arguments in C is possible. You must remember that code in function will never change arguments. As these arguments in C will call function.





















No comments:

Post a Comment