Lecture_Week_9.1
Lecture_Week_9.1
Lecture_Week_9.1
Recap
float F2C(float f) {
float c= (f – 32.0) * (5.0 / 9.0);
return c;}
• The impact is even greater when the
operation has multiple statements.
Purpose of
Function: Enhance reusability.
int main() {
int num = 10;
changeValue(&num);
printf("%d", num); // Output: 20
}
#include <stdio.h>
// Function to swap two numbers using call by reference
void swapByReference(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
Example of }
printf("Inside swap: a = %d, b = %d\n", *a, *b);
Reference
int x = 10, y = 20;
printf("Before swap: x = %d, y = %d\n", x, y);
swapByReference(&x, &y); // Call by reference
printf("After swap: x = %d, y = %d\n", x, y);
return 0;}
Output: Before swap: x = 10, y = 20
Inside swap: a = 20, b = 10
After swap: x = 20, y = 10
Command line argument in C
int main() {
• main() is mostly defined with a return type of int and without parameters. ...
}
• Command-line arguments are the values given after the name of the program in the command-
line.
• To pass command-line arguments, we define main() with two arguments: the first argument is
the number of command-line arguments and the second is a list of command-line
arguments. int main(int argc, char *argv[]) { /* ... */ }
or
int main(int argc, char **argv) { /* ... */ }
• Here,
• argc (ARGument Count) is an integer variable that stores the number of command-line
arguments passed by the user including the name of the program.
• argv (ARGument Vector) is an array of character pointers listing all the arguments.
• If argc is greater than zero, the array elements from argv[0] to argv[argc-1] will contain
pointers to strings.
Command line argument in C
$ ./a.out
int main(int argc, char* argv[])
Program Name Is: ./a.out
{printf("Program name is: %s", argv[0]);
No Extra Command Line Argument
if (argc == 1) Passed Other Than Program Name
printf("\nNo Extra Command Line Argument Passed");
if (argc >= 2) { $ ./a.out First Second Third
printf("\nNumber Of Arguments Passed: %d", argc);
Program Name Is: ./a.out
printf("\n----Following Are CLI Arguments Passed----"); Number Of Arguments Passed: 4
----Following Are CLI Arguments
for (int i = 0; i < argc; i++)
Passed----
printf("\nargv[%d]: %s", i, argv[i]); argv[0]: ./a.out
argv[1]: First
}return 0;} argv[2]: Second
argv[3]: Third
Function
with Two
arguments
Function
with no
return value
Function
with return
value
Functions with
Arrays
Function
with 1-D
array
Passing
Values
between
Functions
Passing
Values
between
Functions
Functions
with 2D
arrays
Functions
with 2D
arrays Cont..
Functions
with 2D
arrays Cont..
Macros and Inline functions
• Macros and Inline Functions:
• Use function if the sequence of steps is long
• If small, use macros or inline function, to eliminate the need for retyping and time
overhead.
• Need #define compiler directive
• Find the area of a triangle (given: base and height)
5. Write a function to check whether a number can be expressed as the sum of two
prime numbers
You may use a separate method to check primality
Upcoming Slides
• Recursion