Sunday, October 18, 2015

Functions and Arguments/Parameters in C programming Language


I’m coming back! Long time no see “codingers” wkwk
In this time, I will explain about something that can make u more easier to write a program with C programming language. Guess what? Yeah! FUNCTIONS! Check this out, guys!
Programs that written in C programming language is divided by many blocks of code that called functions. Functions makes all programs are easy to write, read, and maintain. Functions in C programming language is statement or block of code that performs a task. The general form of function in C programming language is :
                Return_type function_name( parameter list )
                {
                body of the function;
                }
                Return_type is data type of the value that functions returns.
                function_name  is the name of the function
                parameter list is a list of parameters. Parameter is a special kind of variable. Parameter is optional, we can call function without parameter.
                body of the function containts statements that define what the function does.
There are 2 types of function:
1.       Predefined standard library functions
Predefined standard library functions are functions which are defined in C library and header files, so we just call them whenever if we need to use them. Example of Predefined standard library functions are scanf(), printf(), puts(), gets(), main(), etc.
C Header files
<ctype.h>
<math.h>
<stdio.h>
<stdlib.h>
<string.h>
<time.h>
2.       User defined functions
User defined functions are functions which are defined by the user that written the program.
There are 3 aspects in C functions:
No.
C function aspects
General syntax
Description
1.
Function definition
Return_type function_name (parameter list); {
Body of the function;
}
·         Function definition contains codes to make the program perform specific task.
·         All statements that contains in function definition will be executed.
·         Function definition has two components: Function declarator and function body.
2.
Function call
Function_name (parameter list);
·         Funtion call calls the actual function.
·         Function call is short term used.
·         Function definition is “Actual Broad Elaboration” of function call.
3.
Function declaration
Return_type function_name (parameter list);
·         The function declaration also called function prototype.
·         Function declaration gives compiler the information about function name, type of arguments to be passed, and return value’s data type.

There are two ways that a function can be called from the program:
                (NOTE:
1.       Actual Parameter
This paramater is used in function call. The parameter’s value provide while the calling a function is known as actual parameter.
2.       Formal Parameter
This parameter is used in function definition. While declaring the function, the argument list of parameters we specify are known as formal parameter. )
a.       Call by value
In the call by value the actual arguments are copied to the formal arguments, any operation performed by function on arguments doesn’t affect actual parameters. The value of the actual parameter can not be modified by fomal parameter. The value of the variable is passed to the function as a parameter. This is the example of call by value.

b.      Call by reference
Different with call by value, in the call by reference, the addreses of actual parameters are passed to the formal parameters, any operation performed on formal parameters affect the value of actual parameters. The value of the actual parameter can be modified by fomal parameter. This is the example of call  by reference.

There are the advantages of using functions in C programming language:
1.       Reduce the size of codes.
2.       Improve the reusebility of the code, same function can be used in any program.
3.       Make debugging and editing code more easier.
4.       Functions can call other functions & call itself (recursive) also.
There are still more advantages of using functions in C programming language. I can’t mention it one by one wkwk
Have a great time with coding, guys! See you in the next posts!

No comments:

Post a Comment