Macros With Arguments: C Program

In this video tutorial lets see how to write macros with arguments. We’ll illustrate finding biggest of 2 numbers by using “Macros with arguments” concept.

Note: Do not leave space between macro template name and the argument list / parenthesis. Also make sure to write the macro expansion inside parenthesis.

Related Read:
Preprocessors In C Programming Language
C Program To Find Area of Circle Using Macro Expansion

Video Tutorial: Macros With Arguments: C Program (Biggest of 2 Numbers)


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

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

Source Code: Macros With Arguments: C Program

#include<stdio.h>

#define MAX(x, y) ( x > y ? x : y )

int main()
{
    int a, b;

    printf("Enter 2 numbers\n");
    scanf("%d%d", &a, &b);

    printf("Biggest of %d and %d is %d\n", a, b, MAX(a, b));

    return 0;
}

Output 1:
Enter 2 numbers
14
5
Biggest of 14 and 5 is 14

Output 2:
Enter 2 numbers
5
14
Biggest of 5 and 14 is 14

Functions return result after evaluating the expression, but Macros are different. Before compiler compiles the program, preprocessor replaces all the occurrence of Macro template with Macro expansion. So in macros, it won’t return the result, but instead the entire macro expansion gets placed in place of macro template.

So in above C program, printf() becomes:

printf("Biggest of %d and %d is %d\n", a, b, ( a > b ? a : b ));

Related Read:
Biggest of Two Numbers Using Ternary Operator: C

Check Below 2 Source code and it’s output

No Parenthesis around macro expansion

#include<stdio.h>

#define SQUARE(n) n * n

int main()
{
  printf("%f", 64/SQUARE(4));

  return 0;
}

Output:
64

Parenthesis around macro expansion

#include<stdio.h>

#define SQUARE(n) (n * n)

int main()
{
  printf("%f", 64/SQUARE(4));

  return 0;
}

Output:
4

In C programming and in mathematics in general, each arithmetic operation has different precedence. Hence by using parenthesis around macro expansion, it gets the highest precedence.

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

Function / Methods In C Programming Language

In today’s video tutorial lets learn the basics of function or methods in C programming language.

Function / Method: is a group of statements that together perform certain task.

Note: Every C program should have 1 main function. No 2 functions / methods should be named as main.

Video Tutorial: Function / Methods In C Programming Language


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

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

Advantages of Function/Methods

1. It provides modularity to the program structure.
2. You can reuse your code. You can write function definition once and call the function any number of times in your program.
3. Easy to read, understand, edit and debug your code.

Types of Functions/Methods

1. Built in functions/methods.
2. User defined functions/methods.

1. Built in functions/methods: So far we’ve used a lot of builtin methods like pow(), sin(), cos(), tan(), sqrt() etc which are all present in library / header file math.h

If you open math.h header file and check the code, it’ll have function prototype and function definition for pow(), sin(), cos(), tan(), sqrt() etc.

Functions like printf(), scanf() and other input output functions/methods are present in stdio.h library file.

Similarly, if we want to write user defined function, we must specify the function prototype and function definition ourselves.

2. User defined functions/methods: We can define and use our own functions. We can have 4 types of user defined functions/methods.
a. No return type, no arguments.
b. No return type, with arguments.
c. With return type, no arguments.
d. With return type, with arguments.

Stay subscribed to our YouTube channel and blog for video tutorials explaining all types of user defined functions.

General Syntax of user defined Funtion/Method

1. Function prototype.
return_type funtion_name(argument_data_type_list);

2. Function definition.
return_type funtion_name(argument_data_type_list)
{
//instructions
}

3. Function Call.
funtion_name(argument_data_type_list);

Source Code: Function / Methods In C Programming Language: With Return Type and With Arguments

#include<stdio.h>

int add(int, int); // function prototype

int main()
{
    int a = 10, b = 20, c;

    c = add(a, b); // function call

    printf("Addition of %d and %d is %d\n", a, b, c);

    return 0;
}

//function definition
int add(int i, int j)
{
    return(i+j);
}

Output
Addition of 10 and 20 is 30

Source Code: Function / Methods In C Programming Language: No Return Type and No Arguments

#include<stdio.h>

void companies(); // function prototype

int main()
{
    companies(); // function call

    return 0;
}

//function definition
void companies()
{
    printf("1. IBM\n");
    printf("2. Apple\n");
    printf("3. Google\n");
    printf("4. Oracle\n");
    printf("5. Ripple\n");
}

Output
1. IBM
2. Apple
3. Google
4. Oracle
5. Ripple

Source Code: Function / Methods In C Programming Language: No Return Type and With Arguments

#include<stdio.h>

void multiply(int, int); // function prototype

int main()
{
    int a = 9, b = 12;

    multiply(a, b); // function call

    return 0;
}

//function definition
void multiply(int x, int y)
{
    printf("%d x %d = %d\n", x, y, (x*y));
}

Output
9 x 12 = 108

Source Code: Function / Methods In C Programming Language: With Return Type and No Arguments

#include<stdio.h>

int subtract(); // function prototype

int main()
{
    printf("Subtraction Result = %d\n", subtract() ); // function call

    return 0;
}

//function definition
int subtract()
{
    int a = 50, b = 25, c;

    c = a - b;

    return(c);
}

Output
Subtraction Result = 25

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!