Macro With Argument v/s Function: C Program

In today’s video tutorial lets see the difference between a Macro with argument and a simple function.

Related Read:
Macros With Arguments: C Program
Function / Methods In C Programming Language

Video Tutorial: Macro With Argument v/s Function Call: C Program


[youtube https://www.youtube.com/watch?v=kVQsekPnbMU]

YouTube Link: https://www.youtube.com/watch?v=kVQsekPnbMU [Watch the Video In Full Screen.]

Source Code: Macro With Argument v/s Function Call: C Program

#include<stdio.h>

#define PI 3.14159265358979323846
#define AREA(r) ( PI * r * r )

double circle_area(float);

int main()
{
    float r;

    printf("Enter radius of Circle\n");
    scanf("%f", &r);

    printf("Area of Circle, using Macro is %f\n", AREA(r));
    printf("Area of Circle, using Function is %f\n", circle_area(r));

    return 0;
}

double circle_area(float r)
{
    return( 3.14159265358979323846 * r * r );
}

Output:
Enter radius of Circle
5
Area of Circle, using Macro is 78.539816
Area of Circle, using Function is 78.539816

As you can see in above output the result is same for Macro and function call. Now the question is, when to use macros and when to use functions?

When to use macros and when to use functions?

Macro: If a macro template is used 100 times inside a C program, all these macro template will be replaced with its macro expansion before its passed to the compiler. This process is called as preprocessing, and it’s done by preprocessor. Because of this, the size of the program increases.

Function: Function definition is written only once and even if its called 100 times inside a program it won’t increase the program size. Whenever there is a function call, the control is passed to the function definition where some logical operation is performed and then a meaningful result is returned back to the calling function. This takes some time.

So the trade off is between memory space and time.

If memory is not an issue, then you can simply use Macros – as it brings in speed of execution as an advantage. But if memory space is an issue – if you’re writing for a mobile device, then it’s better to go with functions. Though its slower, it uses less memory space.

While coding for bigger real-time projects you’ll surely need the knowledge of macros and macros with argument, so know the syntax and it’s advantages. In this fast moving world speed of execution really matters. And C programming specifically is famous and used often for its speed and its easy integration(and interaction) with the hardware devices.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Custom Functions: jQuery

Video tutorial illustrates writing custom functions in jQuery.

Following topics are covered in the video:
Function Declaration.
Function Expression.
Passing parameters.
Returning value.
Returning nothing, etc ..

HTML code
index.html

1
2
3
4
5
6
7
8
9
10
11
12
<html>
<head><title>Functions in jQuery!</title></head>
<body>
 
<span></span>
<button id="name">Software</button>
 
 
<script src="script/jquery-1.8.1.min.js" type="text/javascript"></script>
<script src="script/my_script.js" type="text/javascript"></script>
</body>
</html>

Here we have 1 span tag for display purpose and a button with an id name.

jQuery code: Passing Parameter To Function
my_script.js

1
2
3
4
5
6
7
8
9
$(document).ready( function() {
 fun1("Oracle");
});
 
 
function fun1(text){
  $("span").append(text);
  return false;
}

function is a keyword, fun1 is a function name we have given.
Once the webpage is loaded, fun1 function will be called, and Oracle is passed to it as parameter.
Inside fun1(), the span tag is selected and the passed parameter is appended to the span tag.
Since the function doesn’t return any value back, we have written return false.

jQuery code: Passing Parameter To Function And Returning Value
my_script.js

1
2
3
4
5
6
7
8
9
10
$(document).ready( function() {
 
 var d = fun1(10, 20);
 $("span").append(d);
 
});
 
function fun1(a, b){
     return(a+b);
}

Once the webpage is loaded, fun1 function will be called, and 10, 20 is passed to it as parameters.
Inside fun1(), 10 and 20 are added and the resulting value is returned back to the calling function, where in it is stored in a variable called d, where we append it to the span tag.

jQuery code: Function Expression
my_script.js

1
2
3
4
5
6
7
8
$(document).ready( function() {
  $("#name").click(software);
});
 
var software = function() {
 $("span").append("<h1>Microsoft ltd</h1>");
 return false;
}

Here, once the user clicks on the button(with an id name), the custom function software gets invoked.
Since the function here doesn’t return any value, we have written return false;

jQuery code: Function Expression with Event Binding
my_script.js

1
2
3
4
5
6
7
8
$(document).ready( function() {
  $("#name").bind('click', software);
});
 
var software = function() {
 $("span").append("<h1>Microsoft ltd</h1>");
 return false;
}

Here we bind the event click to the button and then invoke the function software.

Video Tutorial: Custom Functions: jQuery


[youtube https://www.youtube.com/watch?v=P_68D1foqXg]

YouTube Link: https://www.youtube.com/watch?v=P_68D1foqXg [Watch the Video In Full Screen.]



Main Purposes of writing a function:
Re-usability of code.
Reduce the number of lines of code.
Cleaner looking code.
We could easily trace, modify and maintain the code.

Helps in Object Oriented Programming, as the data is bind to methods.
We could write the code in such a way that, we could pass different parameters to the same function and get desired results. Hence reducing a lot of code overhead and cost of writing extra useless codes!