class_6_programming_in_c_winter202425
class_6_programming_in_c_winter202425
#i n c l u d e < s t d i o . h>
return 0;
}
We can write a function doing a similar job, (though trivial in this particular
case, we will still take it up to illustrate the point!),
The rst line of a function denition tells the compiler the following:
3. The arguments it takes. (In this case no arguments and hence void).
The above is not yet in completely usabel form. A complete workable program
can be made the following way:
#i n c l u d e < s t d i o . h>
1
{
printMessage ();
return 0;
}
Other functions are executed only when a specc call to them is made.
Reusability
#i n c l u d e < s t d i o . h>
return 0;
}
#i n c l u d e < s t d i o . h>
2
return 0;
}
We have already seen use of arguments with the way we used printf and
scanf.
Instead of just printing programming is fun, you could print whatever you
gave as argument using the printf function.
#i n c l u d e < s t d i o . h>
return 0;
}
3
// Function to find the minimum value in an array
#i n c l u d e < s t d i o . h>
m i n S c o r e = minimum ( scores );
return 0;
Recursive Functions
The factorial example
#i n c l u d e < s t d i o . h>
unsigned int j ;
unsigned long int factorial ( unsigned int n);
4
printf ("%2u ! = %l u \ n " , j , factorial ( j )); return 0; } // Rec
if ( n == 0 ) result = 1;
else
result = n * factorial (n = 1); // Notice here that we are ca
return result ;
Questions
1. How do you create user dened libraries? Create a user dened library
for computing prime numbers. Compute prime numbers till a user input
number using this function from the newly created library.
2. Write a function which receives a oat and an int from main( ), nds the
product of these two and returns the product which is printed through
main( ).