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.
printf("Biggest of %d and %d is %d\n", a, b, MAX(a, b));
return 0;
}
#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.
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.
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 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
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);
}
#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);
}
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);
}
$(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.
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
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!