C program to pass different types of arguments to a function :
In this C programming tutorial, we will learn how to pass different types of arguments to a function and how to print their values. This program is for beginner level and you will get the basic understanding of how a function works in C.
What our program will do :
- Suppose we are working on an application. The application is only for users above 18 years. Our program will verify the age and print out one message to the user.
- The program will ask to enter name and age.
- Then it will pass the name and age (both are different types of arguments) to a different function. This function will verify the age and print one message.
C program :
#include <stdio.h>
//5
void validateUserAge(char* name,int age){
//6
if(age < 18){
printf("Sorry %s! You cannot register on this Application.\n",name);
}else{
printf("Hello %s! Welcome to our Application.\n",name);
}
}
int main()
{
//1
char name[100];
int age;
//2
printf("Enter your name : ");
scanf("%s",name);
//3
printf("Enter age : ");
scanf("%d",&age);
//4
validateUserAge(name,age);
return 0;
}
Explanation :
The commented numbers in the above program denote the step number below :
- name is a character array that will hold the user input string.age is an integer to hold the age.
- Ask the user to enter the name. Read and save it in name variable.
- Similarly, ask the user to enter the age. Save it in variable age.
- Call validateUserAge function to validate the age.
- validateUserAge function takes two different types of inputs. The first one is character pointer variable and second one is integer variable.
- Check if the age is less than 18 or not. If yes, print that he cannot register on the application, else greet the user.
Output :
Enter the name : Albert
Enter age : 17
Sorry Albert! You cannot register on this Application.
Enter the name : Alex
Enter age : 27
Hello Alex! Welcome to our Application.