22
C Programming Functions

Function in c

Embed Size (px)

DESCRIPTION

 

Citation preview

  • 1. C Programming Functions
  • 2. Introduction A function is a block of code performing a specific task which can be called from other parts of a program. The name of the function is unique in a C Program and is Global. It means that a function can be accessed from any location with in a C Program. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returns nothing.
  • 3. Program to find if the number is Armstrong or not #include #include int cube(int); void main() { clrscr(); int num, org, remainder,sum=0; printf("Enter any numbern"); scanf("%d",&num); org=num; while(num!=0) { remainder=num%10; sum=sum+cube(remainder); num=num/10; } if(org==sum) { printf("nThe number is armstrong"); } else { printf("nThe number is not armstrong"); } getch(); } int cube(int n) { int qbe; qbe=n*n*n; return qbe; }
  • 4. Second way: #include int sum_cube_digits(int n) #include { int sum_cube_digits(int); int rem ,sum=0; void main() { while(n!=0) clrscr(); { int num, org, f_call; remainder=n%10; printf("Enter any numbern"); sum=sum+rem*rem*rem; scanf("%d",&num); org=num; n=n/10; f_call=sum_cube_digits(num); } if(org==f_call) return sum; { printf("nThe number is armstrong"); } } else { printf("nThe number is not armstrong"); } getch(); }
  • 5. Third way: #include #include int armstrong(int); void main() { clrscr(); int num; printf("Enter any numbern"); scanf("%d",&num); armstrong(num); getch(); } int armstrong(int n) { int remainder,sum=0,org; org=n; while(n!=0) { remainder=n%10; sum=sum+remainder*remainder*remaind er; n=n/10; } if(org==sum) { printf("nThe number is armstrong"); } else { printf("nThe number is not armstrong"); } return(0); }
  • 6. Program to calculate factorial of a number. #include #include int calc_factorial (int); // ANSI function prototype void main() { clrscr(); int number; printf("Enter a numbern"); scanf("%d", &number); calc_factorial (number);// argument number is passed getch(); } int calc_factorial (int i) { int loop, factorial_number = 1; for (loop=1; loop num2) result = num1; else result = num2; return result; }
  • 11. Local and global variables Local: These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main program. As such, they are normally implemented using a stack. Local variables cease to exist once the function that created them is completed. They are recreated each time a function is executed or called. Global: These variables can be accessed by any function comprising the program. They do not get recreated if the function is recalled. To declare a global variable, declare it outside of all the functions. There is no general rule for where outside the functions these should be declared, but declaring them on top of the code is normally recommended for reasons of scope. If a variable of the same name is declared both within a function and outside of it, the function will use the variable that was declared within it and ignore the global one.
  • 12. Function Arguments: If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function. The formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit. While calling a function, there are two ways that arguments can be passed to a function: Call Type Description Call by value This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. Call by reference This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.
  • 13. Fibonacci series in c using for loop #include int main() { int n, first = 0, second = 1, next, c; printf("Enter the number of termsn"); scanf("%d",&n); printf("First %d terms of Fibonacci series are :-n",n); for ( c = 0 ; c < n ; c++ ) { if ( c